diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 4b4ca89b5..6241dceae 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -580,8 +580,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() vartok = tok->tokAt(4); else if (Token::Match(tok, "if ( %var% == NULL|0 )|&&")) vartok = tok->tokAt(2); - else if (Token::Match(tok, "if|while ( %var% )|&&") && - !Token::simpleMatch(tok->tokAt(4)->link(), "} else")) + else if (Token::Match(tok, "if|while ( %var% )|&&")) vartok = tok->tokAt(2); else continue; @@ -601,7 +600,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() // start token = inside the if-body const Token *tok1 = tok->next()->link()->tokAt(2); - if (Token::Match(tok, "if|while ( %var% )")) + if (Token::Match(tok, "if|while ( %var% )|&&")) { // pointer might be null null = false; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index c96ecec51..96a59d282 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -830,6 +830,12 @@ 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" + " } else { *p = 0; }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str()); + check("void foo(abc *p) {\n" " if (!p) {\n" " }\n"