diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 325503caa..8c312e8fd 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -636,6 +636,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() vartok = tok->tokAt(3); else if (Token::Match(tok, "if|while ( %var% )|&&")) vartok = tok->tokAt(2); + else if (Token::Match(tok, "if ( ! ( %var% =")) + vartok = tok->tokAt(4); else continue; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f2c3174ac..1aad74c93 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1177,6 +1177,14 @@ private: " return fred->a;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); + + // #2789 - assign and check pointer + check("void f() {\n" + " char *p;\n" + " if (!(p=x())) { }\n" + " *p = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str()); } // Test CheckNullPointer::nullConstantDereference