Uninitialized variables: fixed false positive in while loop

This commit is contained in:
Daniel Marjamäki 2013-06-27 18:28:00 +02:00
parent 6b01d2d5a8
commit 6997d38881
2 changed files with 16 additions and 5 deletions

View File

@ -1503,14 +1503,16 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
} else { } else {
if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP())) if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
usetok = tok; usetok = tok;
else if (tok->strAt(1) == "=") { else {
bool assign = true; bool assign = true;
if (tok->strAt(1) == "=") {
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId() == var.varId()) { if (tok2->varId() == var.varId()) {
assign = false; assign = false;
break; break;
} }
} }
}
if (assign) if (assign)
return true; return true;
} }

View File

@ -2842,6 +2842,15 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("void f() {\n"
" int x;\n"
" while (a) {\n"
" init(&x);\n"
" x++;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void f() {\n" checkUninitVar2("void f() {\n"
" int x;\n" " int x;\n"
" while (a) {\n" " while (a) {\n"