Menu

knownConditionTrueFalse not triggered when global variable is involved

2019-12-06
2019-12-10
  • Hubert Melchert

    Hubert Melchert - 2019-12-06

    I recently discovered a bug in my code that cppcheck hasn't warned me about. In my opinion cppcheck should be fully aware of this error . In simpified provided example below cppcheck is silent when it comes to if condition being always false:

    typedef enum
    {
        testEnum_a = 1,
        testEnum_b = 2
    }testEnum_t;
    
    testEnum_t e = 3;
    
    int main(void)
    {
        if(e & testEnum_a & testEnum_b)
        {
            printf("True\n");
        }
        else
        {
            printf("False\n");
        }
    
        return 0;
    }
    

    If i remove variable "e" from that condition cppcheck warns me about always false condition. When I prepend definition of "e" with static cppcheck works as expected.

    static testEnum_t e = 3;
    
    int main(void)
    {
        if(e & testEnum_a & testEnum_b)
        {
            printf("True\n");
        }
        else
        {
            printf("False\n");
        }
    
        return 0;
    }
    
    cppcheck --enable=all test.c
    Checking test.c ...
    test.c:15:23: style: Condition 'e&testEnum_a&testEnum_b' is always false [knownConditionTrueFalse]
        if(e & testEnum_a & testEnum_b)
                          ^
    

    When I move defition of "e" to main cppcheck detects always false condition.
    So why cppcheck can't warn about knowConditionTrueFalse when there is a global variable with external linkage involved? Regardless of actual value of "e" condition will always be false.

     

    Last edit: Hubert Melchert 2019-12-06
  • versat

    versat - 2019-12-10

    You are right, testEnum_a & testEnum_b will always be false, so Cppcheck could warn even if the value of e is not known. Maybe some check to avoid false positives avoids this warning to be generated, but I do not know. I think this false negative is worth a ticket.

     

    Last edit: versat 2019-12-10
  • versat

    versat - 2019-12-10

    While trying to reproduce the issue and finding a minimal example I found out something.
    When the if condition is changed to testEnum_a & testEnum_b & e the issue is reported:

    $ ./cppcheck --enable=style forum.c
    Checking forum.c ...
    forum.c:11:33: style: Condition 'testEnum_a&testEnum_b&e' is always false [knownConditionTrueFalse]
        if( testEnum_a & testEnum_b & e)
                                    ^
    

    So it matters where exactly the unknown/unsure variable value is in the condition. But in this case that does not make a difference and the issue should be detected regardless of the order of the variables in the condition.

    Edit: I have created a ticket: https://trac.cppcheck.net/ticket/9527
    Thanks for reporting that issue.

     

    Last edit: versat 2019-12-10
  • Hubert Melchert

    Hubert Melchert - 2019-12-10

    Can you create a ticket based on this post? If not then I will create a Trac account and report this.

     
    • versat

      versat - 2019-12-10

      I have created a ticket: https://trac.cppcheck.net/ticket/9527

       

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.