Fixed #2345 (False positive: uninitialized variable (in sub-condition in if inside a loop))

This commit is contained in:
Daniel Marjamäki 2010-12-23 09:15:45 +01:00
parent b89f486d0d
commit aff3623fec
2 changed files with 37 additions and 0 deletions

View File

@ -895,6 +895,24 @@ private:
{
if (tok->str() == "{" || tok->str() == "}" || tok->str() == "for")
return;
if (Token::simpleMatch(tok, "if ("))
{
// bail out all variables that are used in the condition
unsigned int parlevel = 0;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel == 0)
break;
--parlevel;
}
else if (tok2->varId())
ExecutionPath::bailOutVar(checks, tok2->varId());
}
}
const Token *next = parse(*tok, checks);
tok = next->next();
}

View File

@ -767,6 +767,25 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
// Ticket #2345: False positive in sub-condition in if inside a loop
checkUninitVar("void f(int x) {\n"
" const PoolItem* pItem;\n"
" while (x > 0) {\n"
" if (GetItem(&pItem) && (*pItem != rPool))\n"
" { }\n"
" x--;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f(int x) {\n"
" const PoolItem* pItem;\n"
" while (x > 0) {\n"
" if (*pItem != rPool)\n"
" { }\n"
" x--;\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: pItem\n", errout.str());
}
// switch..