From 0a28b7309ffbcfd27a807f5a1adfe2eaffb5cdcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Mar 2011 15:02:06 +0100 Subject: [PATCH] Fixed #2641 (False positive: Possible null pointer dereference (global pointer, function call)) --- lib/checknullpointer.cpp | 9 +++++++++ test/testnullpointer.cpp | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 4681177d6..bb4850ac9 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -448,6 +448,15 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() else if (indentlevel2 == 0 && tok2->str() == "return") break; + // Function call: If the pointer is a global variable it + // might be changed by the call. + // TODO: false negatives if the pointer is local. + else if (Token::Match(tok2, "[;{}] %var% (") && + Token::simpleMatch(tok2->tokAt(2)->link(), ") ;")) + { + break; + } + // Check if pointer is null. // TODO: false negatives for "if (!p || .." else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 635973e0c..a5d0525cc 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -326,6 +326,15 @@ private: " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #2641 - global pointer, function call + check("ABC *abc;\n" + "void f() {\n" + " abc->a = 0;\n" + " do_stuff();\n" + " if (abc) { }\n" + "}"); + ASSERT_EQUALS("",errout.str()); } // Dereferencing a pointer and then checking if it is null