Stumbling over a nontrivial bool script in C ++

When Cppcheck runs this code, [1] it complains about the error :

void bool_express(bool aPre, bool aX, bool aPost) { bool x; const bool pre = aPre; if (pre) { x = aX; } const bool post = aPost; // error checking passage of the original code: if ( !pre || !x || !post ) { if ( !pre ) { trace("pre failed"); } else if ( !x ) { // <-- HERE Cppcheck complains trace("x failed"); } else { trace("post failed"); } } else { // success passage of the original code: trace("ok"); } } 

This is the message that makes me nervous:

 Id: uninitvar Summary: Uninitialized variable: x Message: Uninitialized variable: x 

I think this is a false positive, but I admit that this may not be obvious. However, I do not want to touch this code because it is old and much heavier than this extracted version.

Have you ever encountered similar situations? How to deal with this?




[1] This code comes down to its bones, the source code sets the precondition ( pre ), does something ( x ), then forces the postcondition ( post ), after which some error conditions are checked. I converted the results of the execution of the functions that are called and then stored in pre , x and post in 3 arguments of my test case.

+1
c ++ cppcheck false-positive
Aug 08 '16 at 13:03
source share
3 answers

Static analysis seems to complain, because if pre is false, then x never set.

Your code is structured in such a way that the value x never opens if pre is false - I would say that the static analyzer does not provide useful output in this case.

Listing the different cases that we have (so we can be sure that this is cppcheck, not us!):

  • The first statement in which the call x is in the line if ( !pre || !x || !post ) is due to a short circuit rating : if( A || B || C ) does not evaluate B or C if A true ; so we never try to read uninitialised x (since x is only uninitialized if pre is false, in which case we stopped evaluating the expression!)

  • The second use is in

    if ( !pre ) { trace("pre failed"); } else if ( !x ) { // <-- HERE Cppcheck complains

    Again, we can get into the violation line if pre was true (in this case x correctly initialized).

From this we can conclude that either:

  • The actual code mistakenly tries to read x in some state, even if pre is false, and you skipped it when building the example (sometimes the program logic flow may be a little dumb)

  • The static analyzer is lazy and puts the else if( !x ) and cannot determine if this line is available with an uninitialized value.

From the code you provided, you should not worry: the static analysis tool is technically correct that x can be uninitialized, but is not used in those cases (and therefore should probably not be warned).

I would recommend setting x to the default if you are not sure, or if the actual logic is extremely stupid.

+1
Aug 08 '16 at 13:52 on
source share

In Cppcheck, this is false positive . I solved this by adding inline suppression: [1]

  if ( !pre ) { trace("pre failed"); // cppcheck-suppress uninitvar } else if ( !x ) { trace("x failed"); } else { trace("post failed"); } 

and I also got his attention from Cppcheck developers:

# 7663 (False positive: uninitialized variable in unreachable code)




[1] I decided not to initialize the variable. This is not for performance reasons, but for information on fixing a bug in a future version when Cppcheck says

 Id: unmatchedSuppression Summary: Unmatched suppression: uninitvar Message: Unmatched suppression: uninitvar 
+1
Aug 08 '16 at 2:03
source share

If your pre condition is false , than x will not be initialized. On the if ( !x ) CppCheck warns about using an undefined value. To fix this, initialize the variable x .

0
Aug 08 '16 at 13:42 on
source share



All Articles