Fixed #5503 (FP: Uninitialized variable - initialize in in if and else branch)

This commit is contained in:
Daniel Marjamäki 2016-01-30 20:48:28 +01:00
parent 923f7f843d
commit b908bb18a9
2 changed files with 20 additions and 1 deletions

View File

@ -205,6 +205,14 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<unsigned
}
else if (tok->isComparisonOp()) {
if (tok->values.size() == 1U && tok->values.front().isKnown()) {
if (tok->values.front().intvalue)
*alwaysTrue = true;
else
*alwaysFalse = true;
return;
}
const Token *vartok, *numtok;
if (tok->astOperand2() && tok->astOperand2()->isNumber()) {
vartok = tok->astOperand1();
@ -404,7 +412,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
return true;
}
if (alwaysTrue && noreturnIf)
if (alwaysTrue && (initif || noreturnIf))
return true;
std::map<unsigned int, VariableValue> varValueIf;

View File

@ -778,6 +778,17 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo(int x)\n" // #5503
"{\n"
" int i;\n"
" if (x < 2)\n"
" i = 22;\n"
" else if (x >= 2)\n" // condition is always true
" i = 33;\n"
" return i;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo()\n"
"{\n"
" int i;\n"