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

View File

@ -2842,6 +2842,15 @@ private:
"}");
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"
" int x;\n"
" while (a) {\n"