diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index e4173bd84..4b4ca89b5 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -574,13 +574,13 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() // vartok : token for the variable const Token *vartok = 0; - if (Token::Match(tok, "if ( ! %var% ) {")) + if (Token::Match(tok, "if ( ! %var% )|&&")) vartok = tok->tokAt(3); - else if (Token::Match(tok, "if ( NULL|0 == %var% ) {")) + else if (Token::Match(tok, "if ( NULL|0 == %var% )|&&")) vartok = tok->tokAt(4); - else if (Token::Match(tok, "if ( %var% == NULL|0 ) {")) + else if (Token::Match(tok, "if ( %var% == NULL|0 )|&&")) vartok = tok->tokAt(2); - else if (Token::Match(tok, "if|while ( %var% ) {") && + else if (Token::Match(tok, "if|while ( %var% )|&&") && !Token::simpleMatch(tok->tokAt(4)->link(), "} else")) vartok = tok->tokAt(2); else diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f6e124fa9..c96ecec51 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -823,6 +823,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + check("void foo(char *p) {\n" + " if (p && *p == 0) {\n" + " }\n" + " printf(\"%c\", *p);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); + check("void foo(abc *p) {\n" " if (!p) {\n" " }\n"