Fix issue 9395 and 9423: False positive: nullPointerRedundantCheck (#2323)

This commit is contained in:
Paul Fultz II 2019-11-04 00:02:25 -06:00 committed by Daniel Marjamäki
parent c207828a11
commit acd2a92efc
2 changed files with 31 additions and 1 deletions

View File

@ -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())

View File

@ -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"