Skip to content

Commit e1cfdb9

Browse files
Fix #12742 FP containerOutOfBounds for increment in loop (danmar#8230)
1 parent ba14d48 commit e1cfdb9

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4531,7 +4531,7 @@ struct ConditionHandler {
45314531
}
45324532

45334533
// Variable changed in loop code
4534-
const Token* const start = top;
4534+
const Token* const start = top->strAt(-1) == "for" ? top->astOperand2() : top; // skip init statement
45354535
const Token* const block = top->link()->next();
45364536
const Token* const end = block->link();
45374537

test/teststl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,12 @@ class TestStl : public TestFixture {
10101010
ASSERT_EQUALS(
10111011
"[test.cpp:2:12] -> [test.cpp:4:21]: (warning) Either the condition 'col>textline.size()' is redundant or 'col' can have the value textline.size(). Expression 'textline[col]' causes access out of bounds. [containerOutOfBounds]\n",
10121012
errout_str());
1013+
1014+
check("void f(const std::vector<int>& v) {\n" // #12742
1015+
" for (unsigned i = 0; i < v.size();)\n"
1016+
" (void)v[i++];\n"
1017+
"}\n");
1018+
ASSERT_EQUALS("", errout_str());
10131019
}
10141020

10151021
void outOfBoundsIndexExpression() {

0 commit comments

Comments
 (0)