Menu

Cppcheck failing to understand assert path

2021-06-09
2021-06-09
  • Curtis Malainey

    Curtis Malainey - 2021-06-09

    I have a small program here that seems to detect a bug in cppcheck 2.4.1
    which arises with code like panic and asserts that the dump state then loop
    the system forever.

    struct test {
            int i;
    }
    
    void fail() {
            while(1){}
    }
    
    int main(){
            struct test *test_p = NULL;
    
            void(test_p || fail());
            test_p->i = 0;
    }
    

    Which results in the following error

    main.c:13:2: error: Null pointer dereference: test_p [nullPointer]
    test_p->i = 0;
    ^
    main.c:10:24: note: Assignment 'test_p=NULL', assigned value is 0
    struct test *test_p = NULL;
    ^
    main.c:13:2: note: Null pointer dereference
    test_p->i = 0;
    ^

    Even though it is clear that it is impossible for main to reach the
    dereference when it is NULL.

    Originally discovered here with the task_next variable
    https://github.com/thesofproject/sof/blob/main/src/schedule/edf_schedule.c#L98

    Using the command cppcheck --platform=unix32 --force --max-configs=4096 -q
    --inline-suppr --suppress=variableScope --suppress=unusedFunction
    -isrc/arch/xtensa -Isrc/include --std=c89 -itest -j 16 src/

    Curtis

     

    Last edit: Daniel Marjamäki 2021-06-09
  • Daniel Marjamäki

    Thanks! I would suggest that if possible, don't expand assert in Cppcheck analysis. The FP is fixed if I write assert there instead. And if assert is seen Cppcheck understands the semantics better and can warn about side effects in assert condition also.

     

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.