Skip to content

Commit a2ee326

Browse files
authored
Fix 11530: FP arrayIndexOutOfBounds with array of functions (cppcheck-opensource#5191)
1 parent 4f466a5 commit a2ee326

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

lib/forwardanalyzer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,14 +796,15 @@ struct ForwardTraversal {
796796
return Break();
797797
return Break();
798798
} else if (Token* callTok = callExpr(tok)) {
799+
// TODO: Dont traverse tokens a second time
800+
if (start != callTok && tok != callTok && updateRecursive(callTok->astOperand1()) == Progress::Break)
801+
return Break();
799802
// Since the call could be an unknown macro, traverse the tokens as a range instead of recursively
800803
if (!Token::simpleMatch(callTok, "( )") &&
801804
updateRange(callTok->next(), callTok->link(), depth - 1) == Progress::Break)
802805
return Break();
803806
if (updateTok(callTok) == Progress::Break)
804807
return Break();
805-
if (start != callTok && updateRecursive(callTok->astOperand1()) == Progress::Break)
806-
return Break();
807808
tok = callTok->link();
808809
if (!tok)
809810
return Break();

test/testbufferoverrun.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class TestBufferOverrun : public TestFixture {
175175
TEST_CASE(array_index_70); // #11355
176176
TEST_CASE(array_index_71); // #11461
177177
TEST_CASE(array_index_72); // #11784
178+
TEST_CASE(array_index_73); // #11530
178179
TEST_CASE(array_index_multidim);
179180
TEST_CASE(array_index_switch_in_for);
180181
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -1940,6 +1941,17 @@ class TestBufferOverrun : public TestFixture {
19401941
ASSERT_EQUALS("", errout.str());
19411942
}
19421943

1944+
// #11530
1945+
void array_index_73()
1946+
{
1947+
check("void f() {\n"
1948+
" int k = 0;\n"
1949+
" std::function<void(int)> a[1] = {};\n"
1950+
" a[k++](0);\n"
1951+
"}\n");
1952+
ASSERT_EQUALS("", errout.str());
1953+
}
1954+
19431955
void array_index_multidim() {
19441956
check("void f()\n"
19451957
"{\n"

0 commit comments

Comments
 (0)