Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4911,10 +4911,13 @@ static bool valueFlowForLoop2(const Token *tok,
execute(secondExpression, &programMemory, &result, &error);
}

memory1->swap(startMemory);
if (memory1)
memory1->swap(startMemory);
if (!error) {
memory2->swap(endMemory);
memoryAfter->swap(programMemory);
if (memory2)
memory2->swap(endMemory);
if (memoryAfter)
memoryAfter->swap(programMemory);
}

return true;
Expand Down Expand Up @@ -5229,6 +5232,11 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {
const Token* condTok = getCondTokFromEnd(endBlock);
if (scope && condTok)
programMemoryParseCondition(pm, condTok, nullptr, getSettings(), scope->type != Scope::eElse);
if (condTok && Token::simpleMatch(condTok->astParent(), ";")) {
ProgramMemory endMemory;
if (valueFlowForLoop2(condTok->astTop()->previous(), nullptr, &endMemory, nullptr))
pm.replace(endMemory);
}
// ProgramMemory pm = pms.get(endBlock->link()->next(), getProgramState());
for (const auto& p:pm.values) {
nonneg int varid = p.first;
Expand Down
15 changes: 15 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class TestBufferOverrun : public TestFixture {
TEST_CASE(array_index_51); // #3763
TEST_CASE(array_index_52); // #7682
TEST_CASE(array_index_53); // #4750
TEST_CASE(array_index_54); // #10268
TEST_CASE(array_index_multidim);
TEST_CASE(array_index_switch_in_for);
TEST_CASE(array_index_for_in_for); // FP: #2634
Expand Down Expand Up @@ -1559,6 +1560,20 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("[test.cpp:7]: (error) Array 'M[3][1]' accessed at index M[*][2], which is out of bounds.\n", errout.str());
}

void array_index_54() {
check("void f() {\n"
" g(0);\n"
"}\n"
"void g(unsigned int x) {\n"
" int b[4];\n"
" for (unsigned int i = 0; i < 4; i += 2) {\n"
" b[i] = 0;\n"
" b[i+1] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void array_index_multidim() {
check("void f()\n"
"{\n"
Expand Down