Skip to content

Commit 6020feb

Browse files
authored
Fix 11461: arrayIndexOutOfBounds false positive (danmar#4686)
1 parent a09667a commit 6020feb

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/valueflow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9197,6 +9197,9 @@ static std::vector<ValueFlow::Value> isOutOfBoundsImpl(const ValueFlow::Value& s
91979197
return {};
91989198
if (size.bound == ValueFlow::Value::Bound::Lower)
91999199
return {};
9200+
// Checking for underflow doesnt mean it could be out of bounds
9201+
if (indexValue->intvalue == 0)
9202+
return {};
92009203
ValueFlow::Value value = inferCondition(">=", indexTok, indexValue->intvalue);
92019204
if (!value.isKnown())
92029205
return {};

test/testbufferoverrun.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class TestBufferOverrun : public TestFixture {
195195
TEST_CASE(array_index_68); // #6655
196196
TEST_CASE(array_index_69); // #6370
197197
TEST_CASE(array_index_70); // #11355
198+
TEST_CASE(array_index_71); // #11461
198199
TEST_CASE(array_index_multidim);
199200
TEST_CASE(array_index_switch_in_for);
200201
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -1912,6 +1913,19 @@ class TestBufferOverrun : public TestFixture {
19121913
ASSERT_EQUALS("[test.cpp:3]: (error) Array 'a[5]' accessed at index 5, which is out of bounds.\n", errout.str());
19131914
}
19141915

1916+
// #11461
1917+
void array_index_71()
1918+
{
1919+
check("unsigned int f(unsigned int Idx) {\n"
1920+
" if (Idx < 64)\n"
1921+
" return 0;\n"
1922+
" Idx -= 64;\n"
1923+
" int arr[64] = { 0 };\n"
1924+
" return arr[Idx];\n"
1925+
"}\n");
1926+
ASSERT_EQUALS("", errout.str());
1927+
}
1928+
19151929
void array_index_multidim() {
19161930
check("void f()\n"
19171931
"{\n"

0 commit comments

Comments
 (0)