From 94fc13f0c4b7887da4e5d922220ff3e3d565902d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 Nov 2010 19:21:08 +0100 Subject: [PATCH] Fixed #2159 (False positive: Null pointer dereference 'if (!x) return *y;') --- lib/checknullpointer.cpp | 2 +- test/testnullpointer.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index cc5d9cda2..9a0687e1f 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -461,7 +461,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (Token::Match(tok2, "goto|return|continue|break|throw|if")) { - if (Token::Match(tok2, "return * %var%")) + if (Token::Match(tok2, "return * %varid%", varid)) nullPointerError(tok2, tok->strAt(3)); break; } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 69357d396..b1b3295eb 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -43,7 +43,7 @@ private: TEST_CASE(nullpointer7); TEST_CASE(nullpointer8); TEST_CASE(nullpointer9); - TEST_CASE(nullpointer10); // check if pointer is null and then dereference it + TEST_CASE(checkAndDeRef); // check if pointer is null and then dereference it } void check(const char code[]) @@ -631,7 +631,7 @@ private: } // Check if pointer is null and the dereference it - void nullpointer10() + void checkAndDeRef() { check("void foo(char *p) {\n" " if (!p) {\n" @@ -671,6 +671,13 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo(bool p) {\n" + " if (!p) {\n" + " }\n" + " return *x;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // This is why this check can't be used on the simplified token list check("void f(Foo *foo) {\n" " if (!dynamic_cast(foo)) {\n"