Skip to content

Commit eb96e49

Browse files
authored
Fix issue 10268: ValueFlow; Wrong value in for loop (#3257)
1 parent c67e618 commit eb96e49

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

lib/valueflow.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4911,10 +4911,13 @@ static bool valueFlowForLoop2(const Token *tok,
49114911
execute(secondExpression, &programMemory, &result, &error);
49124912
}
49134913

4914-
memory1->swap(startMemory);
4914+
if (memory1)
4915+
memory1->swap(startMemory);
49154916
if (!error) {
4916-
memory2->swap(endMemory);
4917-
memoryAfter->swap(programMemory);
4917+
if (memory2)
4918+
memory2->swap(endMemory);
4919+
if (memoryAfter)
4920+
memoryAfter->swap(programMemory);
49184921
}
49194922

49204923
return true;
@@ -5229,6 +5232,11 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {
52295232
const Token* condTok = getCondTokFromEnd(endBlock);
52305233
if (scope && condTok)
52315234
programMemoryParseCondition(pm, condTok, nullptr, getSettings(), scope->type != Scope::eElse);
5235+
if (condTok && Token::simpleMatch(condTok->astParent(), ";")) {
5236+
ProgramMemory endMemory;
5237+
if (valueFlowForLoop2(condTok->astTop()->previous(), nullptr, &endMemory, nullptr))
5238+
pm.replace(endMemory);
5239+
}
52325240
// ProgramMemory pm = pms.get(endBlock->link()->next(), getProgramState());
52335241
for (const auto& p:pm.values) {
52345242
nonneg int varid = p.first;

test/testbufferoverrun.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class TestBufferOverrun : public TestFixture {
132132
TEST_CASE(array_index_51); // #3763
133133
TEST_CASE(array_index_52); // #7682
134134
TEST_CASE(array_index_53); // #4750
135+
TEST_CASE(array_index_54); // #10268
135136
TEST_CASE(array_index_multidim);
136137
TEST_CASE(array_index_switch_in_for);
137138
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -1559,6 +1560,20 @@ class TestBufferOverrun : public TestFixture {
15591560
ASSERT_EQUALS("[test.cpp:7]: (error) Array 'M[3][1]' accessed at index M[*][2], which is out of bounds.\n", errout.str());
15601561
}
15611562

1563+
void array_index_54() {
1564+
check("void f() {\n"
1565+
" g(0);\n"
1566+
"}\n"
1567+
"void g(unsigned int x) {\n"
1568+
" int b[4];\n"
1569+
" for (unsigned int i = 0; i < 4; i += 2) {\n"
1570+
" b[i] = 0;\n"
1571+
" b[i+1] = 0;\n"
1572+
" }\n"
1573+
"}\n");
1574+
ASSERT_EQUALS("", errout.str());
1575+
}
1576+
15621577
void array_index_multidim() {
15631578
check("void f()\n"
15641579
"{\n"

0 commit comments

Comments
 (0)