From a6c7ae8d514dd88355961cc0ce4f77ec9a29a313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 Aug 2011 07:51:32 +0200 Subject: [PATCH] Fixed #2789 (null pointer: problems not detected when pointer is assigned and checked if it's null in a if statement) --- lib/checknullpointer.cpp | 2 ++ test/testnullpointer.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) 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