Fixed #4007 (False positive: 'Possible null pointer dereference' when using short-circuit evaluation)

This commit is contained in:
Daniel Marjamäki 2012-10-07 19:06:49 +02:00
parent 6a37942431
commit fefd8529c6
2 changed files with 16 additions and 6 deletions

View File

@ -1175,12 +1175,13 @@ private:
for (it = checks.begin(); it != checks.end(); ++it) {
Nullpointer *c = dynamic_cast<Nullpointer *>(*it);
if (c && c->varId == varid && c->null) {
if ((Token::Match(tok->tokAt(-4), "[=[(,] %var% && * %var% ,|]|)|;|=|%op%") ||
Token::Match(tok->tokAt(-5), "[=[(,] ! %var% %oror% * %var% ,|]|)|;|=|%op%") ||
Token::Match(tok->tokAt(-4), "return|case %var% && * %var% ,|:|;|=|%op%") ||
Token::Match(tok->tokAt(-5), "return|case ! %var% %oror% * %var% ,|:|;|=|%op%")) &&
tok->tokAt(-3) && (tok->tokAt(-3)->varId() == tok->varId()))
return;
for (const Token *tok2 = tok; tok2 && tok2->str() != ";"; tok2 = tok2->previous()) {
// Checking that pointer is not NULL
if (Token::Match(tok2, "return|=|[|(|,|&& %varid% )| &&", varid))
return;
if (Token::Match(tok2, "return|=|[|(|,|%oror% ! %varid% )| %oror%", varid))
return;
}
CheckNullPointer *checkNullPointer = dynamic_cast<CheckNullPointer *>(c->owner);
if (checkNullPointer) {

View File

@ -55,6 +55,7 @@ private:
TEST_CASE(nullpointer19); // #3811
TEST_CASE(nullpointer20); // #3807 (fp: return p ? (p->x() || p->y()) : z)
TEST_CASE(nullpointer21); // #4038 (fp: if (x) p=q; else return;)
TEST_CASE(nullpointer22); // #4007 (fp: (uri != NULL) && (*uri == 0))
TEST_CASE(nullpointer_castToVoid); // #3771
TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it
TEST_CASE(nullConstantDereference); // Dereference NULL constant
@ -1291,6 +1292,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void nullpointer22() { // #4007 - fp: (x != NULL) && (*x == 0)
check("void f() {\n"
" int *p = 0;\n"
" int result = (p != NULL) && (*p == 0);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void nullpointer_castToVoid() { // #3771
check("void f () {\n"
" int *buf = NULL;\n"