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

View File

@ -1314,6 +1314,20 @@ private:
"}\n");
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..
checkUninitVar("void f()\n"
"{\n"