From 57056bcf6174d98802f005aaf25b7957f8dce3ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 27 Mar 2011 21:29:49 +0200 Subject: [PATCH] Fixed #2689 (False positive: Possible null pointer dereference) --- lib/checknullpointer.cpp | 8 ++++++++ test/testnullpointer.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 242426a62..234bcd54a 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -660,6 +660,14 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (indentlevel == 0) break; --indentlevel; + + if (null && indentlevel == 0) + { + // skip all "else" blocks because they are not executed in this execution path + while (Token::simpleMatch(tok2, "} else {")) + tok2 = tok2->tokAt(2)->link(); + null = false; + } } if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 421aa137b..25d99596f 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1001,6 +1001,15 @@ private: " wilma->Reload();\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void test(int *i) {\n" + " if(i == NULL) {\n" + " int b = 1;\n" + " } else {\n" + " int b = *i;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // Test CheckNullPointer::nullConstantDereference