Skip to content

Commit ed29364

Browse files
Merge pull request diffblue#5128 from romainbrenguier/bugfix/negative-size-arrays
[TG-9313] Fix invariant failure in case of negative size arrays
2 parents 9171c86 + 4259db2 commit ed29364

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Test {
2+
public static void check(Integer t) {
3+
if (t != null)
4+
try {
5+
Integer[] arr = new Integer[-7];
6+
assert false;
7+
} catch (NegativeArraySizeException e) {
8+
assert false;
9+
}
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
Test.class
3+
--function Test.check
4+
line 5 Array size should be >= 0: FAILURE
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
--
8+
--
9+
Test that defining an array with negative size does not crash JBMC

src/goto-symex/field_sensitivity.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,15 @@ exprt field_sensitivityt::get_fields(
180180
#ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
181181
else if(
182182
ssa_expr.type().id() == ID_array &&
183-
to_array_type(ssa_expr.type()).size().id() == ID_constant &&
184-
numeric_cast_v<mp_integer>(
185-
to_constant_expr(to_array_type(ssa_expr.type()).size())) <=
186-
max_field_sensitivity_array_size)
183+
to_array_type(ssa_expr.type()).size().id() == ID_constant)
187184
{
185+
const mp_integer mp_array_size = numeric_cast_v<mp_integer>(
186+
to_constant_expr(to_array_type(ssa_expr.type()).size()));
187+
if(mp_array_size < 0 || mp_array_size > max_field_sensitivity_array_size)
188+
return ssa_expr;
189+
188190
const array_typet &type = to_array_type(ssa_expr.type());
189-
const std::size_t array_size =
190-
numeric_cast_v<std::size_t>(to_constant_expr(type.size()));
191+
const std::size_t array_size = numeric_cast_v<std::size_t>(mp_array_size);
191192

192193
array_exprt result(type);
193194
result.reserve_operands(array_size);

src/solvers/flattening/boolbv_width.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ const boolbv_widtht::entryt &boolbv_widtht::get_entry(const typet &type) const
141141
if(total>(1<<30)) // realistic limit
142142
throw analysis_exceptiont("array too large for flattening");
143143

144-
entry.total_width = numeric_cast_v<std::size_t>(total);
144+
if(total < 0)
145+
entry.total_width = 0;
146+
else
147+
entry.total_width = numeric_cast_v<std::size_t>(total);
145148
}
146149
}
147150
else if(type_id==ID_vector)

0 commit comments

Comments
 (0)