Menu ▾ ▴

Checking test of "constant" function pointer instead of function return

4 days ago
4 days ago
  • Pierrick Garnier

    Hello,

    In the first place, congratulations for this project ! Code analysis tools are really useful for faulty developpers as me. Thanks ! ;)

    Despite the suspect conditional test "if(fn1)", the following piece of code doesn't trigger any result with cppcheck v. 2.22.0 :

    #include <bool.h>
    
    bool fn1(void)
    {
        static bool internalState = false;
    
        internalState = !internalState;
    
        return(internalState);
    }
    
    int main()
    {
        while(1)
        {
            if(fn1)
            {
                printf("1");
            }
        }
    }
    

    As fn1 is a "constant" function pointer (definition is present and not "weak" tagged in the code), the test result will be always true. So, it is probably a "parentheses missing" case. I should have written "if(fn1())".

    Why CppCheck doesn't trigger on that ?

    I know gcc will (and I suppose CLang too), but with some more simple embedded C compilers, it doesn't ! So, it could be really useful that CppCheck warn about this kind of mistakes.

    Do you agree ? 😇

     
  • CHR

    CHR - 4 days ago

    Are you passing --enable=style or --enable=all?

     
  • Pierrick Garnier

    Hello,

    Thanks a lot for your answer. 👍
    I was using the GUI, but using CLI, I've got that :

    C:\[...]>"C:\Program Files\Cppcheck\cppcheck.exe" --enable=all --checkers-report=retour.txt .\test.c
    Checking test.c ...
    test.c:2:2: information: Include file: <bool.h> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
    #include <bool.h>
    ^
    test.c:4:6: style: The function 'fn1' should have static linkage since it is not used outside of its translation unit. [staticFunction]
    bool fn1(void)
    ^
    nofile:0:0: information: Active checkers: 115/189 [checkersReport]
    

    I'm a little surprised to get a new error about static storage, given that I checked "style" and "exhaustive" in the GUI...
    But theres is still not warning about the "if" mistake.

     

    Last edit: Pierrick Garnier 4 days ago
  • Pierrick Garnier

    OMG !

    You're totally right ! 🤩

    I downloaded the last succesful nightly build and there is the result :

    C:\[...]>"C:\[...]\portable\cppcheck.exe" --enable=all --checkers-report=retour.txt .\test.c
    Checking test.c ...
    test.c:2:2: information: Include file: <bool.h> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
    #include <bool.h>
     ^
    **test.c:17:6: style: Condition 'fn1' is always true [knownConditionTrueFalse]
      if(fn1)**
         ^
    test.c:4:6: style: The function 'fn1' should have static linkage since it is not used outside of its translation unit. [staticFunction]
    bool fn1(void)
         ^
    nofile:0:0: information: Active checkers: 115/189 [checkersReport]
    

    Sorry for the "false positive" bug report... 😅

    Please accept my best gratitude for the help you gave me, CHR.

    This topic can be closed, I think.

     
    👍
    1

Log in to post a comment.