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