From 8d16ee946c58fbc2192e0dbed7c9597e934f1117 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 4 May 2022 23:54:44 -0500 Subject: [PATCH] Fix 11027: False positive: containerOutOfBounds from condition in assert (#4084) * Fix 11027: False positive: containerOutOfBounds from condition in assert * Format --- lib/programmemory.cpp | 12 +++--------- test/teststl.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 7ec72eb16..81e1ddc32 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -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(); diff --git a/test/teststl.cpp b/test/teststl.cpp index 3280ad3d9..51f1613de 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -844,6 +844,16 @@ private: "const std::vector A::v = {1, 2};\n"); ASSERT_EQUALS("", errout.str()); + checkNormal("struct a {\n" + " std::vector 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()