Skip to content

Commit 98e22f6

Browse files
authored
Fix 10413: FP arrayIndexOutOfBoundsCond after check (#3446)
1 parent 045f21e commit 98e22f6

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7711,6 +7711,10 @@ static std::vector<ValueFlow::Value> isOutOfBoundsImpl(const ValueFlow::Value& s
77117711
// TODO: Use a better way to decide if the variable in unconstrained
77127712
if (!indexTok->variable() || !indexTok->variable()->isArgument())
77137713
return {};
7714+
if (std::any_of(indexTok->values().begin(), indexTok->values().end(), [&](const ValueFlow::Value& v) {
7715+
return v.isSymbolicValue() && v.isPossible() && v.bound == ValueFlow::Value::Bound::Upper;
7716+
}))
7717+
return {};
77147718
if (indexValue->bound != ValueFlow::Value::Bound::Lower)
77157719
return {};
77167720
if (size.bound == ValueFlow::Value::Bound::Lower)

test/testbufferoverrun.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class TestBufferOverrun : public TestFixture {
135135
TEST_CASE(array_index_56); // #10284
136136
TEST_CASE(array_index_57); // #10023
137137
TEST_CASE(array_index_58); // #7524
138+
TEST_CASE(array_index_59); // #10413
138139
TEST_CASE(array_index_multidim);
139140
TEST_CASE(array_index_switch_in_for);
140141
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -1652,6 +1653,18 @@ class TestBufferOverrun : public TestFixture {
16521653
errout.str());
16531654
}
16541655

1656+
void array_index_59()
1657+
{
1658+
check("long f(long b) {\n"
1659+
" const long a[] = { 0, 1, };\n"
1660+
" const long c = std::size(a);\n"
1661+
" if (b < 0 || b >= c)\n"
1662+
" return 0;\n"
1663+
" return a[b];\n"
1664+
"}\n");
1665+
ASSERT_EQUALS("", errout.str());
1666+
}
1667+
16551668
void array_index_multidim() {
16561669
check("void f()\n"
16571670
"{\n"

0 commit comments

Comments
 (0)