Skip to content

Commit

Permalink
Fix #12406 FP containerOutOfBounds with reassigned vector
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Feb 13, 2024
1 parent d145f9c commit b631798
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6855,6 +6855,19 @@ class TestValueFlow : public TestFixture {
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1));

code = "int f(std::vector<int>& v) {\n"
" std::vector<int> 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()
Expand Down

0 comments on commit b631798

Please sign in to comment.