CheckUninitVar: Fixed false negatives when there are conditions
This commit is contained in:
parent
d301cf28ec
commit
fb0477affd
|
@ -347,7 +347,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
|
||||||
unsigned int condVarId = 0;
|
unsigned int condVarId = 0;
|
||||||
VariableValue condVarValue(0);
|
VariableValue condVarValue(0);
|
||||||
const Token *condVarTok = nullptr;
|
const Token *condVarTok = nullptr;
|
||||||
if (Token::simpleMatch(tok, "if (") &&
|
if (alwaysFalse)
|
||||||
|
;
|
||||||
|
else if (Token::simpleMatch(tok, "if (") &&
|
||||||
astIsVariableComparison(tok->next()->astOperand2(), "!=", "0", &condVarTok)) {
|
astIsVariableComparison(tok->next()->astOperand2(), "!=", "0", &condVarTok)) {
|
||||||
std::map<unsigned int,VariableValue>::const_iterator it = variableValue.find(condVarTok->varId());
|
std::map<unsigned int,VariableValue>::const_iterator it = variableValue.find(condVarTok->varId());
|
||||||
if (it != variableValue.end() && it->second != 0)
|
if (it != variableValue.end() && it->second != 0)
|
||||||
|
@ -383,7 +385,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
|
||||||
if (!tok)
|
if (!tok)
|
||||||
break;
|
break;
|
||||||
if (tok->str() == "{") {
|
if (tok->str() == "{") {
|
||||||
bool possibleInitIf(number_of_if > 0 || suppressErrors);
|
bool possibleInitIf((!alwaysTrue && number_of_if > 0) || suppressErrors);
|
||||||
bool noreturnIf = false;
|
bool noreturnIf = false;
|
||||||
const bool initif = !alwaysFalse && checkScopeForVariable(tok->next(), var, &possibleInitIf, &noreturnIf, alloc, membervar);
|
const bool initif = !alwaysFalse && checkScopeForVariable(tok->next(), var, &possibleInitIf, &noreturnIf, alloc, membervar);
|
||||||
|
|
||||||
|
@ -433,7 +435,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
|
||||||
// goto the {
|
// goto the {
|
||||||
tok = tok->tokAt(2);
|
tok = tok->tokAt(2);
|
||||||
|
|
||||||
bool possibleInitElse(number_of_if > 0 || suppressErrors);
|
bool possibleInitElse((!alwaysFalse && number_of_if > 0) || suppressErrors);
|
||||||
bool noreturnElse = false;
|
bool noreturnElse = false;
|
||||||
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar);
|
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar);
|
||||||
|
|
||||||
|
|
|
@ -2402,6 +2402,24 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar("void f(int x) {\n"
|
||||||
|
" int value;\n"
|
||||||
|
" if (x == 32)\n"
|
||||||
|
" value = getvalue();\n"
|
||||||
|
" if (x == 1)\n"
|
||||||
|
" v = value;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: value\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar("void f(int x) {\n"
|
||||||
|
" int value;\n"
|
||||||
|
" if (x == 32)\n"
|
||||||
|
" value = getvalue();\n"
|
||||||
|
" if (x == 32) {}\n"
|
||||||
|
" else v = value;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: value\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("static int x;" // #4773
|
checkUninitVar("static int x;" // #4773
|
||||||
"int f() {\n"
|
"int f() {\n"
|
||||||
" int y;\n"
|
" int y;\n"
|
||||||
|
|
Loading…
Reference in New Issue