diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 433523d46..1afc733e3 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1175,12 +1175,13 @@ private: for (it = checks.begin(); it != checks.end(); ++it) { Nullpointer *c = dynamic_cast(*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(c->owner); if (checkNullPointer) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 1acc2ec69..d74c2d864 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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"