Uninitialized variables: fixed false positives by bailing out when variable is conditionally initialized and then there is a conditional return.

This commit is contained in:
Daniel Marjamäki 2012-12-27 18:45:00 +01:00
parent bf9b900c30
commit b914466285
2 changed files with 16 additions and 0 deletions

View File

@ -1170,6 +1170,13 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
bool noreturnIf = false;
const bool initif = !alwaysTrue && checkScopeForVariable(scope, tok->next(), var, &possibleInitIf, &noreturnIf);
// bail out for such code:
// if (a) x=0; // conditional initialization
// if (b) return; // cppcheck doesn't know if b can be false when a is false.
// x++; // it's possible x is always initialized
if (!alwaysTrue && noreturnIf && number_of_if > 0)
return true;
std::map<unsigned int, int> varValueIf;
if (!initif) {
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {

View File

@ -2533,6 +2533,15 @@ private:
" if (x || i>0) {}\n" // <- no error
"}\n");
ASSERT_EQUALS("", errout.str());
// Unknown => bail out..
checkUninitVar2("void f(int x) {\n"
" int i;\n"
" if (a(x)) i = 0;\n"
" if (b(x)) return;\n"
" i++;\n" // <- no error if b(x) is always true when a(x) is false
"}\n");
ASSERT_EQUALS("", errout.str());
}
};