Fixed #1389 (false positive: uninitialized variable)

This commit is contained in:
Daniel Marjamäki 2010-02-17 18:10:50 +01:00
parent f005d674ec
commit db2c362604
2 changed files with 27 additions and 0 deletions

View File

@ -113,9 +113,12 @@ static const Token *checkExecutionPaths_(const Token *tok, std::list<ExecutionPa
if (Token::simpleMatch(tok, "do {") && Token::simpleMatch(tok2, "} while (")) if (Token::simpleMatch(tok, "do {") && Token::simpleMatch(tok2, "} while ("))
tok2 = tok2->tokAt(2)->link(); tok2 = tok2->tokAt(2)->link();
// bail out all variables if the scope contains a "return"
// bail out all variables used in this for/while/switch/do // bail out all variables used in this for/while/switch/do
for (; tok && tok != tok2; tok = tok->next()) for (; tok && tok != tok2; tok = tok->next())
{ {
if (tok->str() == "return")
ExecutionPath::bailOut(checks);
if (tok->varId()) if (tok->varId())
ExecutionPath::bailOutVar(checks, tok->varId()); ExecutionPath::bailOutVar(checks, tok->varId());
} }

View File

@ -1462,6 +1462,30 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo(const int iVar, unsigned int slot, unsigned int pin)\n"
"{\n"
" int i;\n"
"\n"
" if (iVar == 0)\n"
" {\n"
" switch (slot)\n"
" {\n"
" case 4: return 5;\n"
" default: return -1;\n"
" }\n"
" }\n"
" else\n"
" {\n"
" switch (pin)\n"
" {\n"
" case 0: i = 2; break;\n"
" default: i = -1; break;\n"
" }\n"
" }\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// while.. // while..
checkUninitVar("int f()\n" checkUninitVar("int f()\n"
"{\n" "{\n"