From acd2a92efc574af545609763954805cdb112c6f0 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 4 Nov 2019 00:02:25 -0600 Subject: [PATCH] Fix issue 9395 and 9423: False positive: nullPointerRedundantCheck (#2323) --- lib/programmemory.cpp | 2 +- test/testnullpointer.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 275cfccb0..0f3328075 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -181,7 +181,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok setvar = true; } } - if (!setvar && (Token::Match(tok2, "[;{}] %var% =") || + if (!setvar && (Token::Match(tok2, ";|{|}|%type% %var% =") || Token::Match(tok2, "[;{}] const| %type% %var% ("))) { const Token *vartok = tok2->next(); while (vartok->next()->isName()) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index c249cf418..ed6b023e3 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -82,6 +82,7 @@ private: TEST_CASE(nullpointer40); TEST_CASE(nullpointer41); TEST_CASE(nullpointer42); + TEST_CASE(nullpointer44); // #9395, #9423 TEST_CASE(nullpointer_addressOf); // address of TEST_CASE(nullpointerSwitch); // #2626 TEST_CASE(nullpointer_cast); // #4692 @@ -1537,6 +1538,35 @@ private: errout.str()); } + void nullpointer44() { + // #9395 + check("int foo( ) {\n" + " const B* b = getB();\n" + " const double w = ( nullptr != b) ? 42. : 0.0;\n" + " if ( w == 0.0 )\n" + " return 0;\n" + " return b->get();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // #9423 + check("extern F* GetF();\n" + "extern L* GetL();\n" + "void Foo() {\n" + " const F* const fPtr = GetF();\n" + " const bool fPtrOk = fPtr != NULL;\n" + " assert(fPtrOk);\n" + " if (!fPtrOk)\n" + " return;\n" + " L* const lPtr = fPtr->l;\n" + " const bool lPtrOk = lPtr != NULL;\n" + " assert(lPtrOk);\n" + " if (!lPtrOk)\n" + " return;\n" + " lPtr->Clear();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void nullpointer_addressOf() { // address of check("void f() {\n" " struct X *x = 0;\n"