Menu

False negative for uninitialized variable if third or later parameter

Megan
2020-02-06
2020-02-10
  • Megan

    Megan - 2020-02-06

    Sample test case that shows the problem. If the uninitialized parameter is first or second (func1), cppcheck flags it, but 3rd or later in the parameter list (func2) and no warning is emitted.

    #include <stddef.h>
    
    static int func1(bool * result, size_t * buflen) {
      int newTotal = 5;
    
      if (*result && *buflen >= newTotal)
        newTotal += 1;
    
      return newTotal;
    }
    
    static int func2(bool * result, int num, int num2, size_t * buflen) {
      int newTotal = 5;
    
      if (*result && *buflen >= newTotal)
        newTotal += 1;
    
      return newTotal;
    }
    
    int main() {
      size_t bytesCopied;
      bool copied_all = true;
    
      int result1 = func1(&copied_all, &bytesCopied); // proper warning emitted
      int result2 = func2(&copied_all, 5, 6, &bytesCopied); // no warning emitted
    
      return result1 + result2;
    }
    
     
  • Daniel Marjamäki

    Thanks! I created this ticket: https://trac.cppcheck.net/ticket/9631

    For information, I am hoping that the new "bug hunting" analysis will detect all uninitialized variables in the near future.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.