Menu

V2.6 false positive: knownConditionTrueFalse when break out of loop.

2021-11-24
2021-11-27
  • David Zanter

    David Zanter - 2021-11-24

    Hello, I did see a couple of other false positives related to knownConditionTrueFalse in the Forum; However they didn't seem to be exactly the same. The following code is flagging a false positive of: knownConditionTrueFalse

    This is a simplfied test program, where it looks like it is missing that the second if statement can break out and then pool != NULL.

    void main ()
    {
       void * pool = NULL;
       void * edp = NULL;
       do
       {
          pool = malloc(40);
          if (pool == NULL)
          {
             break;
          }
          edp = malloc(20);
          if (edp == NULL)
          {
             break;
          }
    
          edp->pool               = pool;
          pool                    = NULL;
       }
       while (0);
    
       if (pool)
         free(pool);
    }
    
    [centos@dzdocker tk-build-container]$ cppcheck --enable=all /u/dazant/docker/falsepositive.c
    Checking /u/dazant/docker/falsepositive.c ...
    /u/dazant/docker/falsepositive.c:25:8: style: Condition 'pool' is always false [knownConditionTrueFalse]
       if (pool)
           ^
    /u/dazant/docker/falsepositive.c:21:33: note: Assignment 'pool=NULL', assigned value is 0
          pool                    = NULL;
                                    ^
    /u/dazant/docker/falsepositive.c:25:8: note: Condition 'pool' is always false
       if (pool)
           ^
    

    Thanks!

     

    Last edit: David Zanter 2021-11-24
  • Daniel Marjamäki

    Thanks! I can reproduce. I have created https://trac.cppcheck.net/ticket/10625

     

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.