diff --git a/src/checkother.cpp b/src/checkother.cpp index 4e4808424..0c462ab89 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1111,14 +1111,14 @@ void CheckOther::nullPointerConditionalAssignment() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // 1. Initialize.. - if (!tok->Match(tok, "; %var% = 0 ;")) + if (!Token::Match(tok, "; %var% = 0 ;")) continue; const unsigned int varid(tok->next()->varId()); if (!varid) continue; - for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) + for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next()) { if (tok2->str() == "{" || tok2->str() == "}") break; @@ -1141,8 +1141,12 @@ void CheckOther::nullPointerConditionalAssignment() if (Token::Match(tok2, "else !!if")) break; - if (Token::Match(tok2, "%varid% . %var% (", varid)) - nullPointerError(tok2, tok->next()->str()); + if (Token::Match(tok2, "%varid%", varid)) + { + if (Token::Match(tok2, "%varid% . %var% (", varid)) + nullPointerError(tok2, tok->next()->str()); + break; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index 91b14ea77..172ecc1f4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -899,6 +899,15 @@ private: " p->abcd();\n" "}\n"); ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str()); + + // no false positive.. + checkNullPointer("static void foo()\n" + "{\n" + " Foo *p = 0;\n" + " p = new Foo;\n" + " p->abcd();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void checkOldStylePointerCast(const char code[])