Menu

False positive "Condition data==NULL is always true"

2025-11-02
7 days ago
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2025-11-02

    I have noted false positive:

    data=NULL;
     resize(m.size1D,m.size2D);
     if(data==NULL) {size1D=size2D=0;return;}
    

    ** "Condition data==NULL is always true"**

    CppCheck misses a template function resize that sets data to non NULL.

     //this method allocates a new data structure with a given size
    template <class FLOAT>
    void matrix<FLOAT>::resize(TSIZE x, TSIZE y)
    {
     .........
     if((data=(FLOAT **)calloc(y,sizeof(FLOAT *))) == NULL) //pointers to rows
     {
     size2D=size1D=0;       //allocation error
     RaiseError(MatrixId|No_Memory,this);
     return;
     }
     //memset(data,0,y*sizeof(FLOAT *)) is not needed, calloc clears memory;
     size2D=y;
     size1D=x;
    
     ........
     return;
    }
    

    It is strange, because a same situation occurs on line 35 and no "problem" is reported here.

     

    Last edit: Jaroslav Fojtik 2025-11-02
  • Daniel Marjamäki

    Thanks!
    A friend created this ticket: https://trac.cppcheck.net/ticket/14252
    We should fix Cppcheck. But you can workaround these false positives if you make your code consistent.

    To avoid the false positive you can explicitly specify or drop specification of the template parameter for both declaration and definition. It should not missmatch. Either use matrix<FLOAT> or just matrix in both declaration and definition.

     

Log in to post a comment.