From 0c2f2e1c3893055cfbb5d4c667bc34dd04b649d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 Aug 2011 21:57:23 +0200 Subject: [PATCH] Null pointers: Fixed false negative when struct is dereferenced in condition --- lib/checknullpointer.cpp | 4 +++- test/testnullpointer.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 8c312e8fd..cf529a37f 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -379,9 +379,11 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() } // dereference in condition - else if (Token::Match(tok1, "if ( %var% .")) + else if (Token::Match(tok1, "if ( !| %var% .")) { tok1 = tok1->tokAt(2); + if (tok1->str() == "!") + tok1 = tok1->next(); } // dereference in function call (but not sizeof|decltype) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 1aad74c93..f6bff5771 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -231,6 +231,14 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str()); + check("void f(ABC *abc) {\n" + " if (abc->x == 0) {\n" + " return;\n" + " }\n" + " if (!abc);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 5\n", errout.str()); + // TODO: False negative if member of member is dereferenced check("void foo(ABC *abc) {\n" " abc->next->a = 0;\n"