Skip to content

Array theory: gracefully handle negative out-of-bounds access #6772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions regression/cbmc/Array_UF21/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int main()
{
int x[0] = {};
int a = x[-1];
x[a] = 42;
}
9 changes: 9 additions & 0 deletions regression/cbmc/Array_UF21/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c
--arrays-uf-always --bounds-check
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
^warning: ignoring
^Invariant check failed
8 changes: 4 additions & 4 deletions src/solvers/flattening/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,13 @@ void arrayst::add_array_constraints_array_constant(
// We have a constant index - just pick the element at that index from the
// array constant.

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

const exprt v = operands[i];
const exprt v = operands[*i];
DATA_INVARIANT(
index_expr.type() == v.type(),
"array operand type should match array element type");
Expand Down