Fix 10410: FP nullPointerRedundantCheck on if (a || b ) followed by if ( b ) (#3427)

This commit is contained in:
Paul Fultz II 2021-08-29 08:39:41 -05:00 committed by GitHub
parent 06249c08f3
commit 2ee880752f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -361,7 +361,6 @@ void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& vars) void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& vars)
{ {
ProgramMemory pm = state; ProgramMemory pm = state;
fillProgramMemoryFromConditions(pm, tok, settings);
for (const auto& p:vars) { for (const auto& p:vars) {
nonneg int exprid = p.first; nonneg int exprid = p.first;
const ValueFlow::Value &value = p.second; const ValueFlow::Value &value = p.second;
@ -369,6 +368,7 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va
if (value.varId) if (value.varId)
pm.setIntValue(value.varId, value.varvalue); pm.setIntValue(value.varId, value.varvalue);
} }
fillProgramMemoryFromConditions(pm, tok, settings);
ProgramMemory local = pm; ProgramMemory local = pm;
fillProgramMemoryFromAssignments(pm, tok, local, vars); fillProgramMemoryFromAssignments(pm, tok, local, vars);
replace(pm, tok); replace(pm, tok);

View File

@ -119,6 +119,7 @@ private:
TEST_CASE(nullpointer77); TEST_CASE(nullpointer77);
TEST_CASE(nullpointer78); // #7802 TEST_CASE(nullpointer78); // #7802
TEST_CASE(nullpointer79); // #10400 TEST_CASE(nullpointer79); // #10400
TEST_CASE(nullpointer80); // #10410
TEST_CASE(nullpointer_addressOf); // address of TEST_CASE(nullpointer_addressOf); // address of
TEST_CASE(nullpointerSwitch); // #2626 TEST_CASE(nullpointerSwitch); // #2626
TEST_CASE(nullpointer_cast); // #4692 TEST_CASE(nullpointer_cast); // #4692
@ -2441,6 +2442,20 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void nullpointer80() // #10410
{
check("int f(int* a, int* b) {\n"
" if( a || b ) {\n"
" int n = a ? *a : *b;\n"
" if( b )\n"
" n++;\n"
" return n;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer_addressOf() { // address of void nullpointer_addressOf() { // address of
check("void f() {\n" check("void f() {\n"
" struct X *x = 0;\n" " struct X *x = 0;\n"