Fixed #2401 (false positive: Uninitialized variable: result)

This commit is contained in:
Daniel Marjamäki 2011-01-05 20:44:04 +01:00
parent 68de938d23
commit 21af64049c
2 changed files with 34 additions and 12 deletions

View File

@ -600,7 +600,7 @@ private:
{
// sizeof/typeof doesn't dereference. A function name that is all uppercase
// might be an unexpanded macro that uses sizeof/typeof
if (Token::Match(&tok, "sizeof|typeof (") || isUpper(tok.str()))
if (Token::Match(&tok, "sizeof|typeof ("))
return tok.next()->link();
// deallocate pointer
@ -690,8 +690,22 @@ private:
{
if (Token::Match(tok2->tokAt(-2), "[(,] *") || Token::Match(tok2->next(), ". %var%"))
{
if (use_dead_pointer(checks, tok2))
ExecutionPath::bailOutVar(checks, tok2->varId());
// find function call..
const Token *functionCall = tok2;
while (0 != (functionCall = functionCall ? functionCall->previous() : 0))
{
if (functionCall->str() == "(")
break;
if (functionCall->str() == ")")
functionCall = functionCall->link();
}
functionCall = functionCall ? functionCall->previous() : 0;
if (functionCall)
{
if (functionCall->isName() && !isUpper(functionCall->str()) && use_dead_pointer(checks, tok2))
ExecutionPath::bailOutVar(checks, tok2->varId());
}
}
// it is possible that the variable is initialized here

View File

@ -637,15 +637,15 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int test(int cond1, int cond2) {\n"
" int foo;\n"
" if (cond1 || cond2) {\n"
" if (cond2)\n"
" foo = 0;\n"
" }\n"
" if (cond2) {\n"
" int t = foo*foo;\n"
" }\n"
checkUninitVar("int test(int cond1, int cond2) {\n"
" int foo;\n"
" if (cond1 || cond2) {\n"
" if (cond2)\n"
" foo = 0;\n"
" }\n"
" if (cond2) {\n"
" int t = foo*foo;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
@ -1366,6 +1366,14 @@ private:
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
ASSERT_EQUALS("", errout.str());
// #2401 - unknown function/macro might init the variable
checkUninitVar("int f() {\n"
" int x;\n"
" INIT(x);\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// using uninitialized function pointer..
checkUninitVar("void foo()\n"
"{\n"