Fixed #953 (false positive: 'uninitialized variable' with function pointer)

This commit is contained in:
Daniel Marjamäki 2009-11-15 12:42:04 +01:00
parent b5cbc509f3
commit 822f64ec9a
2 changed files with 34 additions and 0 deletions

View File

@ -1361,6 +1361,32 @@ static const Token *uninitvar_checkscope(const Token * const tokens, const Token
} }
} }
// function call via function pointer
if (Token::Match(tok, "( * %var% ) ("))
{
// is the variable passed as a parameter to some function?
unsigned int parlevel = 0;
for (const Token *tok2 = tok->link()->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel <= 1)
break;
--parlevel;
}
else if (tok2->varId() == varid)
{
// it is possible that the variable is initialized here
init = true;
return 0;
}
}
}
if (tok->str() == "return") if (tok->str() == "return")
{ {
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) for (const Token *tok2 = tok; tok2; tok2 = tok2->next())

View File

@ -1142,6 +1142,14 @@ private:
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int f(int (*assign)(char *p))\n"
"{\n"
" int i;\n"
" (*assign)(&i);\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// arrays.. // arrays..
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"