Skip to content

Commit acd2a92

Browse files
pfultz2danmar
authored andcommitted
Fix issue 9395 and 9423: False positive: nullPointerRedundantCheck (cppcheck-opensource#2323)
1 parent c207828 commit acd2a92

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/programmemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
181181
setvar = true;
182182
}
183183
}
184-
if (!setvar && (Token::Match(tok2, "[;{}] %var% =") ||
184+
if (!setvar && (Token::Match(tok2, ";|{|}|%type% %var% =") ||
185185
Token::Match(tok2, "[;{}] const| %type% %var% ("))) {
186186
const Token *vartok = tok2->next();
187187
while (vartok->next()->isName())

test/testnullpointer.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class TestNullPointer : public TestFixture {
8282
TEST_CASE(nullpointer40);
8383
TEST_CASE(nullpointer41);
8484
TEST_CASE(nullpointer42);
85+
TEST_CASE(nullpointer44); // #9395, #9423
8586
TEST_CASE(nullpointer_addressOf); // address of
8687
TEST_CASE(nullpointerSwitch); // #2626
8788
TEST_CASE(nullpointer_cast); // #4692
@@ -1537,6 +1538,35 @@ class TestNullPointer : public TestFixture {
15371538
errout.str());
15381539
}
15391540

1541+
void nullpointer44() {
1542+
// #9395
1543+
check("int foo( ) {\n"
1544+
" const B* b = getB();\n"
1545+
" const double w = ( nullptr != b) ? 42. : 0.0;\n"
1546+
" if ( w == 0.0 )\n"
1547+
" return 0;\n"
1548+
" return b->get();\n"
1549+
"}\n");
1550+
ASSERT_EQUALS("", errout.str());
1551+
// #9423
1552+
check("extern F* GetF();\n"
1553+
"extern L* GetL();\n"
1554+
"void Foo() {\n"
1555+
" const F* const fPtr = GetF();\n"
1556+
" const bool fPtrOk = fPtr != NULL;\n"
1557+
" assert(fPtrOk);\n"
1558+
" if (!fPtrOk)\n"
1559+
" return;\n"
1560+
" L* const lPtr = fPtr->l;\n"
1561+
" const bool lPtrOk = lPtr != NULL;\n"
1562+
" assert(lPtrOk);\n"
1563+
" if (!lPtrOk)\n"
1564+
" return;\n"
1565+
" lPtr->Clear();\n"
1566+
"}\n");
1567+
ASSERT_EQUALS("", errout.str());
1568+
}
1569+
15401570
void nullpointer_addressOf() { // address of
15411571
check("void f() {\n"
15421572
" struct X *x = 0;\n"

0 commit comments

Comments
 (0)