CheckUninitVar: Fixed false negatives when there are conditions

This commit is contained in:
Daniel Marjamäki 2015-11-21 10:00:21 +01:00
parent d301cf28ec
commit fb0477affd
2 changed files with 24 additions and 4 deletions

View File

@ -347,8 +347,10 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
unsigned int condVarId = 0;
VariableValue condVarValue(0);
const Token *condVarTok = nullptr;
if (Token::simpleMatch(tok, "if (") &&
astIsVariableComparison(tok->next()->astOperand2(), "!=", "0", &condVarTok)) {
if (alwaysFalse)
;
else if (Token::simpleMatch(tok, "if (") &&
astIsVariableComparison(tok->next()->astOperand2(), "!=", "0", &condVarTok)) {
std::map<unsigned int,VariableValue>::const_iterator it = variableValue.find(condVarTok->varId());
if (it != variableValue.end() && it->second != 0)
return true; // this scope is not fully analysed => return true
@ -383,7 +385,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (!tok)
break;
if (tok->str() == "{") {
bool possibleInitIf(number_of_if > 0 || suppressErrors);
bool possibleInitIf((!alwaysTrue && number_of_if > 0) || suppressErrors);
bool noreturnIf = false;
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 {
tok = tok->tokAt(2);
bool possibleInitElse(number_of_if > 0 || suppressErrors);
bool possibleInitElse((!alwaysFalse && number_of_if > 0) || suppressErrors);
bool noreturnElse = false;
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar);

View File

@ -2402,6 +2402,24 @@ private:
"}");
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
"int f() {\n"
" int y;\n"