Menu

false positive in cppcheck 2.5: knownConditionTrueFalse

Jo
2021-07-16
2021-07-16
  • Jo

    Jo - 2021-07-16

    Using the latest cppcheck 2.5 I came across a new false positive I would like to share. With option --enable=all it is reporting knownConditionTrueFalse, where obviously incorrect as my simplified test case proves:

    struct S {
        void* obj;
    };
    
    static int func(struct S a, struct S b) {
        if (a.obj && b.obj) {
            if (a.obj != b.obj) {
                /* according to cppcheck, we never get here ... */
                return 0;
            }
            return 1;
        }
        return 1;
    }
    int main(int ac, char** av) {
        struct S a;
        struct S b;
        a.obj = (void*)1;
        b.obj = (void*)2;
        return func(a,b);
    }
    
    # cppcheck --enable=all foo.c
    Checking foo.c ...
    foo.c:7:12: style: Condition 'a.obj!=b.obj' is always false [knownConditionTrueFalse]
     if (a.obj != b.obj) {
               ^
    foo.c:6:10: note: Assuming condition 'a.obj' is true
        if (a.obj && b.obj) {
             ^
    foo.c:7:12: note: Condition 'a.obj!=b.obj' is always false
     if (a.obj != b.obj) {
               ^
    

    But the code compiles fine and indeed returns 0 from func:

    # gcc foo.c -o foo && ./foo 
    # echo $?
    0
    

    It'd be great if someone could have a look into this & file a bug report for this regression.
    Thanks!

     

    Last edit: Jo 2021-07-16
  • Jo

    Jo - 2021-07-16

    Here's another variant of possibly the same (?) issue:

    int main(int ac, char** av) {
        int strict = ac;
        if (strict && av) {
            if (strict == 1) {
                return 1;
            }
        }
        return 0;
    }   
    

    again: code compiles fine and proves that cppcheck is reporting a false positive here:

    # cppcheck  --enable=all foo2.c
    Checking foo2.c ...
    foo2.c:4:13: style: Condition 'strict==1' is always true [knownConditionTrueFalse]
     if (strict == 1) {
                ^
    foo2.c:3:9: note: Assuming condition 'strict' is true
        if (strict && av) {
            ^
    foo2.c:4:13: note: Condition 'strict==1' is always true
     if (strict == 1) {
                ^
    # gcc foo2.c -o foo2 && ./foo2 bar
    # echo $?
    0
    
     

    Last edit: Jo 2021-07-16

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.