When Cppcheck runs over this code,[1] it complains about an 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 it's a false positive, but I admit that this may not be obvious. Nevertheless I don't want to touch that code, because it's old and a lot heavier than this extracted version.
Have you ever experienced situations like this? How to deal with it?
[1] This code is reduced to its bones, the original code establishes a precondition (pre
), does something (x
), then forces a postcondition (post
), after that, some error conditions are checked. I transformed the runtime-results of the functions that are called and then stored in pre
, x
, and post
into the 3 arguments of my test case.
x
is used uninitialized, if Cppcheck has this option post it and maybe they see something everyone else is missing, although it looks like a false positive to me – Occultationx
to false? I cannot imagine a realistic scenario in which that would blow up. – DanetteDid you make sure the reduced version still produces the CppCheck warning
, yes, I did. – Termitarium