Uninitialized variables: Fixed false positive for 'if (ptr)'

This commit is contained in:
Daniel Marjamäki 2009-12-26 16:21:53 +01:00
parent a82a085d10
commit ab7ffd2391
2 changed files with 22 additions and 1 deletions

View File

@ -1419,6 +1419,8 @@ private:
continue; continue;
if (mode == 2 && !c->pointer) if (mode == 2 && !c->pointer)
continue; continue;
if (mode == 3 && (c->pointer && c->alloc))
continue;
CheckOther *checkOther = dynamic_cast<CheckOther *>(c->owner); CheckOther *checkOther = dynamic_cast<CheckOther *>(c->owner);
if (checkOther) if (checkOther)
@ -1450,6 +1452,11 @@ private:
use(foundError, checks, tok, 2); use(foundError, checks, tok, 2);
} }
static void use_not_pointer(bool &foundError, std::list<ExecutionPath *> &checks, const Token *tok)
{
use(foundError, checks, tok, 3);
}
const Token *parse(const Token &tok, bool &foundError, std::list<ExecutionPath *> &checks) const const Token *parse(const Token &tok, bool &foundError, std::list<ExecutionPath *> &checks) const
{ {
// Variable declaration.. // Variable declaration..
@ -1650,7 +1657,7 @@ private:
bool foundError = false; bool foundError = false;
if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)|[")) if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)|["))
use(foundError, checks, &tok); use_not_pointer(foundError, checks, &tok);
else if (Token::Match(&tok, "!| %var% (")) else if (Token::Match(&tok, "!| %var% ("))
{ {

View File

@ -1314,6 +1314,20 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo()\n"
"{\n"
" char *a;\n"
" if (a);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void foo()\n"
"{\n"
" char *a = malloc(100);\n"
" if (a);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// struct.. // struct..
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"