From f3728c0b7630754a0786a8e41542c64f7ca65f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Mar 2011 20:57:19 +0100 Subject: [PATCH] Fixed #2647 (False positive: Possible null pointer dereference (member function call)) --- lib/checknullpointer.cpp | 10 ++++++++++ test/testnullpointer.cpp | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index a58297c58..9285cef5f 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -418,6 +418,16 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() if (var && (var->isLocal() || var->isArgument())) isLocal = true; + // member function may or may not nullify the pointer if it's global (#2647) + if (!isLocal) + { + const Token *tok2 = tok1; + while (Token::Match(tok2, "%var% .")) + tok2 = tok2->tokAt(2); + if (Token::Match(tok2,"%var% (")) + continue; + } + // count { and } using tok2 unsigned int indentlevel2 = 0; for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next()) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 2b84036d5..2b34878d6 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -336,6 +336,13 @@ private: "}"); ASSERT_EQUALS("",errout.str()); + check("Fred *fred;\n" + "void f() {\n" + " fred->foo();\n" + " if (fred) { }\n" + "}"); + ASSERT_EQUALS("",errout.str()); + // #2641 - local pointer, function call check("void f() {\n" " ABC *abc;\n"