Menu

false positive in cppcheck 2.0

2020-05-11
2020-05-11
  • Cristian Del Fabbro

    Hi,
    I found a false positive in cppcheck 2.0 (previous versions are fine) in the following code:

    #include <iostream>
    
    void do_something(const char ** test) {
        *test = "test";
    }
    
    bool test(int a) {
        bool ok = false;
        const char * r = nullptr;
    
        do_something(&r);
    
        if (r != nullptr) {
            ok = a != 0;
        }
    
        if (ok) {
            std::cout << "TRUE" << std::endl;
        } else {
            std::cout << "FALSE" << std::endl;
        }
        return ok;
    }
    
    
    int main() {
        int c;
        std::cin >> c;
        test(c);
        return 0;
    }
    

    The cppcheck output is:

    cdf@work:~/tmp/$ cppcheck test.cpp --enable=all --std=c++14        
    
    Checking test.cpp ...
    test.cpp:19:6: style: Condition 'ok' is always false [knownConditionTrueFalse]
     if (ok) {
         ^
    test.cpp:9:12: note: Assignment 'ok=false', assigned value is 0
     bool ok = false;
               ^
    test.cpp:19:6: note: Condition 'ok' is always false
     if (ok) {
         ^
    nofile:0:0: information: Cppcheck cannot find all the include files (use --check-config for details) [missingIncludeSystem]
    

    But the variable ok can be true or false:

    cdf@work:~/tmp/$ g++ test.cpp -o test
    cdf@work:~/tmp/$ ./test 
    0
    FALSE
    cdf@work:~/tmp/$ ./test 
    1
    TRUE
    cdf@work:~/tmp/$ 
    
     
  • Daniel Marjamäki

    Thanks! I created this trac ticket: https://trac.cppcheck.net/ticket/9709

     
  • Daniel Marjamäki

    For information, the bug is in the data flow analysis.

     
  • Cristian Del Fabbro

    Thanks!!!

     

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.