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:
parent
bf9b900c30
commit
b914466285
|
@ -1170,6 +1170,13 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
||||||
bool noreturnIf = false;
|
bool noreturnIf = false;
|
||||||
const bool initif = !alwaysTrue && checkScopeForVariable(scope, tok->next(), var, &possibleInitIf, &noreturnIf);
|
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;
|
std::map<unsigned int, int> varValueIf;
|
||||||
if (!initif) {
|
if (!initif) {
|
||||||
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
||||||
|
|
|
@ -2533,6 +2533,15 @@ private:
|
||||||
" if (x || i>0) {}\n" // <- no error
|
" if (x || i>0) {}\n" // <- no error
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue