Skip to content

Commit ec13391

Browse files
authored
Merge pull request #6772 from tautschnig/bugfixes/array-oob
Array theory: gracefully handle negative out-of-bounds access
2 parents b04b4b5 + f769397 commit ec13391

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

regression/cbmc/Array_UF21/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int main()
2+
{
3+
int x[0] = {};
4+
int a = x[-1];
5+
x[a] = 42;
6+
}

regression/cbmc/Array_UF21/test.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--arrays-uf-always --bounds-check
4+
^VERIFICATION FAILED$
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
9+
^Invariant check failed

src/solvers/flattening/arrays.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,13 @@ void arrayst::add_array_constraints_array_constant(
715715
// We have a constant index - just pick the element at that index from the
716716
// array constant.
717717

718-
const std::size_t i =
719-
numeric_cast_v<std::size_t>(to_constant_expr(index));
718+
const optionalt<std::size_t> i =
719+
numeric_cast<std::size_t>(to_constant_expr(index));
720720
// if the access is out of bounds, we leave it unconstrained
721-
if(i >= operands.size())
721+
if(!i.has_value() || *i >= operands.size())
722722
continue;
723723

724-
const exprt v = operands[i];
724+
const exprt v = operands[*i];
725725
DATA_INVARIANT(
726726
index_expr.type() == v.type(),
727727
"array operand type should match array element type");

0 commit comments

Comments
 (0)