Skip to content

Commit

Permalink
Fix 11784: FP arrayIndexOutOfBounds when increment is counted twice (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Jun 24, 2023
1 parent 6c750d9 commit 60321ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ struct ForwardTraversal {
return Break();
} else {
Token* stepTok = getStepTok(tok);
if (updateLoop(end, endBlock, condTok, initTok, stepTok) == Progress::Break)
// Dont pass initTok since it was already evaluated
if (updateLoop(end, endBlock, condTok, nullptr, stepTok) == Progress::Break)
return Break();
}
tok = endBlock;
Expand Down
16 changes: 16 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class TestBufferOverrun : public TestFixture {
TEST_CASE(array_index_69); // #6370
TEST_CASE(array_index_70); // #11355
TEST_CASE(array_index_71); // #11461
TEST_CASE(array_index_72); // #11784
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 @@ -1924,6 +1925,21 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

// #11784
void array_index_72()
{
check("char f(int i) {\n"
" char d[4] = {};\n"
" for (; i < 3; i++) {}\n"
" for (i++; i > 0;) {\n"
" d[--i] = 1;\n"
" break;\n"
" }\n"
" return d[3];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

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

0 comments on commit 60321ed

Please sign in to comment.