Menu

<function> cfg applying to function within a struct. v1.83

Scott H
2018-05-21
2018-05-24
  • Scott H

    Scott H - 2018-05-21

    Hello all,

    Sorry if this has been answered somewhere already. Searches on Google and directly on the forum were both unproductive.

    In our C code we have many cases of the following in our code where the int returned is a status.

    typedef int (*MY_FUNCTION_TYPE) (int Argument);
    
    typedef struct {
        MY_FUNCTION_TYPE MyFunc;
    } MY_STRUCT;
    ...
    
    MY_STRUCT *MyStruct;
    
    // Assign struct pointers...
    
    Status = MyStruct->MyFunc(Args);
    

    In trying to make a CFG to enforce checking this status I made the following rule.

    <function name="MyFunc">
        <use-retval/>
    </function>  
    

    However it only works for the first line below. Not the 2nd. Our code requires it report this error for the 2nd line.

    MyFunc(Args);
    MyStruct->MyFunc(Args);
    

    I also attempted a much longer function definition with every qualified name configuration I could think of. But it gave the same results. (Forgive me, we program entierly in C, so I'm rusty with this programming syntax)

    <function name="MyFunc,MyStruct->MyFunc,MY_FUNCTION_TYPE::MyFunc,MY_STRUCT::MY_FUNCTION_TYPE,MY_STRUCT::MyFunc">
        <use-retval/>
    </function>  
    

    Would anyone know how to get CppCheck to report for our case?

    P.S. - Checking the CppCheck codebase I noticed that there are files for processing classes, but I didn't see one for structs. Is it possible that CppCheck does not understand our particular situation?

    Thank you,
    Scott

     

    Last edit: Scott H 2018-05-21
  • versat

    versat - 2018-05-24

    Even without function pointers i get no warning:
    struct_function.cpp:

    #include <cstdio>
    
    struct teststruct
    {
        int testfunc() { printf("testfunc says hi!\n"); return 1; }
    };
    
    int main()
    {
        teststruct TestStruct1;
        TestStruct1.testfunc();
    }
    

    struct_function.cfg:

    <?xml version="1.0"?>
    <def format="2">
      <function name="teststruct::testfunc">
        <use-retval/>
      </function>
    </def>
    

    output:

    $ g++ struct_function.cpp -o struct_function
    
    $ ./struct_function
    testfunc says hi!
    
    $ ./cppcheck --enable=all --library=struct_function.cfg --check-library --debug-warnings struct_function.cpp
    Checking struct_function.cpp ...
    [struct_function.cpp:5]: (debug) valueflow.cpp:3343:valueFlowFunctionReturn bailout: function return; nontrivial function body
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    

    IMHO this is a false negative.
    You can see that Cppcheck does not complain about a missing configuration for teststruct::testfunc which would be the case if the according function configuration is not present. So this should be the correct way to specify struct functions i guess.

     
  • versat

    versat - 2018-05-24

    I have created ticket 8595.
    Your case is a bit more complex like the one i described in the ticket. Maybe there is a bit more to fix/enhance so it works for your code too, i am not sure.

     
  • versat

    versat - 2018-05-24

    And i nearly forgot about this ticket: https://trac.cppcheck.net/ticket/8290
    I do not know if Cppcheck sees the implementation in your case and i am not sure if Cppcheck is able to track the function pointers, but if it does this could be a reason it ignores the function configuration.

     

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.