Fix 11027: False positive: containerOutOfBounds from condition in assert (#4084)

* Fix 11027: False positive: containerOutOfBounds from condition in assert

* Format
This commit is contained in:
Paul Fultz II 2022-05-04 23:54:44 -05:00 committed by GitHub
parent 5afd6880c3
commit 8d16ee946c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -193,6 +193,8 @@ void ProgramMemory::insert(const ProgramMemory &pm)
mValues.insert(p);
}
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings* settings = nullptr);
static bool evaluateCondition(const std::string& op,
MathLib::bigint r,
const Token* condition,
@ -331,13 +333,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
}
if (!setvar) {
if (!pm.hasValue(vartok->exprId())) {
MathLib::bigint result = 0;
bool error = false;
execute(valuetok, &pm, &result, &error);
if (!error)
pm.setIntValue(vartok, result);
else
pm.setUnknown(vartok);
pm.setValue(vartok, execute(valuetok, pm));
}
}
} else if (tok2->exprId() > 0 && Token::Match(tok2, ".|(|[|*|%var%") && !pm.hasValue(tok2->exprId()) &&
@ -575,8 +571,6 @@ static ValueFlow::Value evaluate(const std::string& op, const ValueFlow::Value&
return result;
}
static ValueFlow::Value execute(const Token* expr, ProgramMemory& pm, const Settings* settings = nullptr);
static ValueFlow::Value executeImpl(const Token* expr, ProgramMemory& pm, const Settings* settings)
{
ValueFlow::Value unknown = ValueFlow::Value::unknown();

View File

@ -844,6 +844,16 @@ private:
"const std::vector<int> A::v = {1, 2};\n");
ASSERT_EQUALS("", errout.str());
checkNormal("struct a {\n"
" std::vector<int> g() const;\n"
"};\n"
"int f(const a& b) {\n"
" auto c = b.g();\n"
" assert(not c.empty());\n"
" int d = c.front();\n"
" return d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void outOfBoundsSymbolic()