Skip to content

Commit 60321ed

Browse files
authored
Fix 11784: FP arrayIndexOutOfBounds when increment is counted twice (#5186)
1 parent 6c750d9 commit 60321ed

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/forwardanalyzer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ struct ForwardTraversal {
671671
return Break();
672672
} else {
673673
Token* stepTok = getStepTok(tok);
674-
if (updateLoop(end, endBlock, condTok, initTok, stepTok) == Progress::Break)
674+
// Dont pass initTok since it was already evaluated
675+
if (updateLoop(end, endBlock, condTok, nullptr, stepTok) == Progress::Break)
675676
return Break();
676677
}
677678
tok = endBlock;

test/testbufferoverrun.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class TestBufferOverrun : public TestFixture {
174174
TEST_CASE(array_index_69); // #6370
175175
TEST_CASE(array_index_70); // #11355
176176
TEST_CASE(array_index_71); // #11461
177+
TEST_CASE(array_index_72); // #11784
177178
TEST_CASE(array_index_multidim);
178179
TEST_CASE(array_index_switch_in_for);
179180
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -1924,6 +1925,21 @@ class TestBufferOverrun : public TestFixture {
19241925
ASSERT_EQUALS("", errout.str());
19251926
}
19261927

1928+
// #11784
1929+
void array_index_72()
1930+
{
1931+
check("char f(int i) {\n"
1932+
" char d[4] = {};\n"
1933+
" for (; i < 3; i++) {}\n"
1934+
" for (i++; i > 0;) {\n"
1935+
" d[--i] = 1;\n"
1936+
" break;\n"
1937+
" }\n"
1938+
" return d[3];\n"
1939+
"}\n");
1940+
ASSERT_EQUALS("", errout.str());
1941+
}
1942+
19271943
void array_index_multidim() {
19281944
check("void f()\n"
19291945
"{\n"

0 commit comments

Comments
 (0)