Uninitialized variables: detect usage of dead pointer: '*p += 10'

This commit is contained in:
Daniel Marjamäki 2010-11-05 17:04:41 +01:00
parent 95bfa50d7e
commit a90a7202cb
2 changed files with 21 additions and 3 deletions

View File

@ -486,9 +486,28 @@ private:
if (Token::Match(tok.tokAt(-2), "[;{}] *"))
{
if (Token::simpleMatch(tok.next(), "="))
init_pointer(checks, &tok);
else
{
// is the pointer used in the rhs?
bool used = false;
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, "[,;=(]"))
break;
else if (Token::Match(tok2, "* %varid%", tok.varId()))
{
used = true;
break;
}
}
if (used)
use_pointer(checks, &tok);
else
init_pointer(checks, &tok);
}
else
{
use_pointer(checks, &tok);
}
return &tok;
}

View File

@ -376,8 +376,7 @@ private:
" char *s = malloc(100);\n"
" *s += 10;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
checkUninitVar("void f()\n"
"{\n"