From 0b09732881e069c0dea1564cf2205960651d888c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 8 Oct 2011 12:58:10 +0200 Subject: [PATCH] Null pointers: fixed false negatives when there are '(p && ..' --- lib/checknullpointer.cpp | 8 ++++---- test/testnullpointer.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 4ee49180c..2efb1b4a4 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -576,12 +576,12 @@ void CheckNullPointer::nullPointerByDeRefAndChec() if (Token::Match(tok1->link()->previous(), "while ( %varid%", varid)) break; - // TODO: there might be false negatives. perhaps - // instead of bailing out it's ok to skip the condition. - // this bailout is related to #3128 if (Token::Match(tok1->link(), "( ! %varid% ||", varid) || Token::Match(tok1->link(), "( %varid% &&", varid)) - break; + { + tok1 = tok1->link(); + continue; + } if (Token::simpleMatch(tok1->link()->previous(), "sizeof (")) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 0a5f7ebda..f05cf858b 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -544,6 +544,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void f(int *p) {\n" + " *p = 12;\n" + " assert(p && (*p<=6));\n" + " if (p) { *p = 0; }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); + check("void foo(x *p)\n" "{\n" " p = p->next;\n"