Menu

cppcheck 1.82 false warnings from builtin_expect !!

2018-06-14
2018-06-15
  • Wilson Snyder

    Wilson Snyder - 2018-06-14

    The following example throws false warnings, caused by the
    branch predictor macro. (Obviously in this case the
    performance of using UNLIKELY is dubious, but there's other
    more performance critical examples I see getting warnings in
    my application.)

    Can this please get fixed in a future drop? Thanks much.

    Thanks

    //Cppcheck 1.82:
    //
    // cppcheck --enable=all bug.cpp
    //[bug.cpp:10] -> [include/cppcheck_bug.cpp:12]: (warning) Either the condition '!fp' is redundant or there is possible null pointer dereference: fp.
    //[bug.cpp:11]: (error) Resource leak: fp
    #include <cstdio>
    #define UNLIKELY(x) __builtin_expect(!!(x), 0)
    void x() {
    FILE* fp = fopen(filenamep, "w");
    if (UNLIKELY(!fp)) { // but just "if (!fp)" works
    return;
    }
    fprintf(fp, "...");
    fclose(fp);
    }

     
  • raynebc

    raynebc - 2018-06-14

    Have you tested this bug in the recently released version 1.84?

     
  • versat

    versat - 2018-06-15

    __builtin_expect is a GCC extension that is not known by default.
    There is a define that "removes" it in gnu.cfg.
    To use the gnu.cfg add --library=gnu to the Cppcheck arguments.
    With Cppcheck 1.84 i still get the warnings if gnu.cfg is used. I guess the three exclamation marks are a bit confusing.

    Telling Cppcheck that UNLIKELY(x) can be ignored (replaced just by x) could help to get rid of the warnings.
    Maybe you can simply add a custom configuration file with an according define, but i have not tested it. If the definition of UNLIKELY is seen then the define in the configuration might be overwritten.

     

    Last edit: versat 2018-06-15

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.