From d76657fec8dddee577fafd8cfd91b6f46910bb14 Mon Sep 17 00:00:00 2001 From: Richard Quirk Date: Wed, 23 Nov 2011 21:19:54 +0100 Subject: [PATCH] Fix false positives for null pointer on exit --- lib/checknullpointer.cpp | 4 ++++ test/testnullpointer.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index e7acbe956..ace4daa7d 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -809,6 +809,10 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() break; } + if (Token::Match(tok2, "exit ( %num% ) ;")) { + break; + } + // parameters to sizeof are not dereferenced if (Token::Match(tok2, "decltype|sizeof (")) { tok2 = tok2->next()->link(); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 1f1f77182..093634aea 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -58,6 +58,7 @@ private: TEST_CASE(nullpointer_in_typeid); TEST_CASE(nullpointer_in_for_loop); TEST_CASE(nullpointerDelete); + TEST_CASE(nullpointerExit); } void check(const char code[], bool inconclusive = false, bool cpp11 = false) { @@ -1607,6 +1608,16 @@ private: "}\n", true); ASSERT_EQUALS("", errout.str()); } + + void nullpointerExit() { + check("void f() {\n" + " K *k = getK();\n" + " if (!k)\n" + " exit(1);\n" + " k->f();\n" + "}\n", true); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestNullPointer)