Uninitialized variables: Fixed false positives when variable x is given a nonzero value when variable y is uninitialized.
This commit is contained in:
parent
abfca0b574
commit
0b4da3d5cd
|
@ -1148,6 +1148,14 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
|||
bool noreturnIf = false;
|
||||
const bool initif = checkScopeForVariable(scope, tok->next(), var, &possibleInitIf, &noreturnIf);
|
||||
|
||||
std::set<unsigned int> notzeroIf;
|
||||
if (!initif) {
|
||||
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
||||
if (Token::Match(tok2, "[;{}] %var% = - %var% ;"))
|
||||
notzeroIf.insert(tok2->next()->varId());
|
||||
}
|
||||
}
|
||||
|
||||
// goto the }
|
||||
tok = tok->link();
|
||||
|
||||
|
@ -1165,6 +1173,14 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
|||
bool noreturnElse = false;
|
||||
const bool initelse = checkScopeForVariable(scope, tok->next(), var, &possibleInitElse, NULL);
|
||||
|
||||
std::set<unsigned int> notzeroElse;
|
||||
if (!initelse) {
|
||||
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
||||
if (Token::Match(tok2, "[;{}] %var% = - %var% ;"))
|
||||
notzeroElse.insert(tok2->next()->varId());
|
||||
}
|
||||
}
|
||||
|
||||
// goto the }
|
||||
tok = tok->link();
|
||||
|
||||
|
@ -1174,8 +1190,11 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
|||
if ((initif && noreturnElse) || (initelse && noreturnIf))
|
||||
return true;
|
||||
|
||||
if (initif || initelse || possibleInitElse)
|
||||
if (initif || initelse || possibleInitElse) {
|
||||
++number_of_if;
|
||||
notzero.insert(notzeroIf.begin(), notzeroIf.end());
|
||||
notzero.insert(notzeroElse.begin(), notzeroElse.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2153,6 +2153,15 @@ private:
|
|||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkUninitVar2("void f() {\n"
|
||||
" int i, y;\n"
|
||||
" if (x) y = -ENOMEM;\n"
|
||||
" else y = get_value(i);\n"
|
||||
" if (y != 0) return;\n" // <- condition is always true if i is uninitialized
|
||||
" i++;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// for, while
|
||||
checkUninitVar2("void f() {\n"
|
||||
" int x;\n"
|
||||
|
|
Loading…
Reference in New Issue