Menu

False positive

2023-11-27
2023-11-27
  • Björn Johansson

    The following code produces a warning about an uninitialized variable: dataArray.
    However if I remove the if statement before the while loop, no warnings are produced. It seems that the sub-scope of the if (or just inserting { } before the while gives the warning) confuses cppcheck about the value of the dataValue variable.

    typedef unsigned int Uint32Type;
    #define DATA_ARRAY_SIZE  (100U)
    
    static Uint32Type init_dataArray(Uint32Type b)
    {
       Uint32Type   dataArray[DATA_ARRAY_SIZE];
       Uint32Type   dataBufCount = 0;
       Uint32Type   dataValue = 0;
       if (b == 0)
       {
          b = 1;
       }
    
       while (dataValue < 100)
       {
          dataArray[dataBufCount] = dataValue;
          dataBufCount++;
          dataValue++;
       }
       return dataArray[0] + b;
    }
    
    int main(void)
    {
       return (int) init_dataArray(2);
    }
    

    Checking cppcheck_mini.c ...
    cppcheck_mini.c:20:11: warning: Uninitialized variable: dataArray [uninitvar]
    return dataArray[0] + b;
    ^
    cppcheck_mini.c:14:21: note: Assuming condition is false
    while (dataValue < 100)
    ^
    cppcheck_mini.c:20:11: note: Uninitialized variable: dataArray
    return dataArray[0] + b;
    ^

     
  • CHR

    CHR - 2023-11-27
     

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.