From b631798f88c6adc568148823bd4210f7674b624c Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 13 Feb 2024 20:20:53 +0100 Subject: [PATCH] Fix #12406 FP containerOutOfBounds with reassigned vector --- lib/valueflow.cpp | 2 ++ test/teststl.cpp | 5 ++++- test/testvalueflow.cpp | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 062dfcc868e..8e3be76260a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2991,6 +2991,8 @@ struct ValueFlowAnalyzer : Analyzer { }); if (value) return {value->intvalue == 0}; + if (!match(tok)) + return {}; ProgramMemory pm = pms.get(tok, ctx, getProgramState()); MathLib::bigint out = 0; if (pm.getContainerEmptyValue(tok->exprId(), out)) diff --git a/test/teststl.cpp b/test/teststl.cpp index 252239307a0..76f3a2565ce 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -586,7 +586,10 @@ class TestStl : public TestFixture { " return true;\n" " return false;\n" "}\n"); - ASSERT_EQUALS("test.cpp:7:style:Consider using std::any_of algorithm instead of a raw loop.\n", errout.str()); + TODO_ASSERT_EQUALS("test.cpp:7:style:Consider using std::any_of algorithm instead of a raw loop.\n", + "test.cpp:8:error:Out of bounds access in expression 'v[i]' because 'v' is empty.\n" + "test.cpp:7 : style : Consider using std::any_of algorithm instead of a raw loop.\n", + errout.str()); checkNormal("bool g();\n" "int f(int x) {\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 2c4b5f8eff7..e560ede0e46 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6855,6 +6855,19 @@ class TestValueFlow : public TestFixture { " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1)); + + code = "int f(std::vector& v) {\n" + " std::vector o = v;\n" + " v.clear();\n" + " for (int i : o) {\n" + " if (i == 1) {\n" + " v.push_back(0);\n" + " }\n" + " }\n" + " auto x = v.empty();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 1)); } void valueFlowContainerElement()