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,9 +690,23 @@ private:
{
if (Token::Match(tok2->tokAt(-2), "[(,] *") || Token::Match(tok2->next(), ". %var%"))
{
if (use_dead_pointer(checks, tok2))
// 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
if (Token::Match(tok2->previous(), "[(,] %var% [,)]"))

View File

@ -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"