Menu

Potential return codes

2023-07-01
2023-07-03
  • Andrew C Aitchison

    This isn't a specific bug, but more a question about philosophy of
    static analysis. If there is a better place to discuss this please
    tell me.

    I am not very familiar with exceptions; if they are a reasonable way
    to resolve this please say so.

    I am programming defensively - allowing for possible cases that
    currently cannot occur - which means that static analysis indicates
    that I am creating unexecutable blocks of code.

    Specifically, I am writing a function which returns a value to
    indicate success, or possibly (which sort of) failure. At present it
    does not fail, always returning "success". Thus if the calling code
    tests the return value, cppcheck warns that the test is always true
    (or false).

    Conceptually, the bug is in the function definition, since it does
    not return all intended values, but currently cppcheck warns me about
    each function call, since they attempt to handle return values that
    are not currently returned.

    It would be better if I could encourage cppcheck to warn me if
    the function always returns the same value, or perhaps if the return
    value is a bool or an enum, when there are values that are never returned.

    Perhaps one way to do this would be a comment listing other potential
    return values that callers should be prepared to handle ?

     
  • Daniel Marjamäki

    Interesting question.

    So in this case you don't want that cppcheck looks at the function definition and determines what the return value is.

    Your suggestion to add a comment is reasonable imho. I am not against to handle some comments or configuration. If possible it should be some kind of standard like microsoft SAL. C++ contracts. ACSL.

    Just a wild idea... if you want you could try to add this code in the function definition:

    #ifdef __cppcheck__
    #define cppcheck_rand_return(MINVAL,MAXVAL) \
        if (rand()%1) return (rand() % (1 + MAXVAL - MINVAL)) + MINVAL;
    #else
    #define cppcheck_rand_return(MINVAL,MAXVAL)
    #endif
    
    // your function
    int dostuff()
    {
         cppcheck_rand_return(-5,-1);
         ... your ordinary code ...
    }
    

    you would need to define __cppcheck__ during cppcheck analysis for that to work. If I understand you correctly the cppcheck_rand_return() should prevent the false positives.

    It would be better if I could encourage cppcheck to warn me if the function always returns the same value, or perhaps if the return value is a bool or an enum, when there are values that are never returned.

    Yes in general it's suspicious if a function always returns the same value.

    Unfortunately I would think that there are situations when a function always returns the same value by intention. For instance in overridden virtual methods and in callback functions.

     
    • Daniel Marjamäki

      I guess you could also hide the function definition for cppcheck. Put the definition in another source file. That should fix the false positive about checking the return value. But maybe you don't want to do that.

       

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.