Skip to content

Commit 941030e

Browse files
committed
get_subexpression_at_offset: do not permit out-of-bounds access
An indexed access must not be out of bounds. This was surfaced by union field sensitivity, which ended up generating SSA symbols with indices beyond array bounds.
1 parent f71c0ae commit 941030e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
KNOWNBUG
1+
CORE
22
main.c
33
--verbosity 10
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$
77
--
88
^warning: ignoring
9-
--
10-
assert(s) fails with field sensitivity, but passes without

src/util/pointer_offset_size.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,17 +633,22 @@ optionalt<exprt> get_subexpression_at_offset(
633633

634634
// no arrays of non-byte-aligned, zero-, or unknown-sized objects
635635
if(
636-
elem_size_bits.has_value() && *elem_size_bits > 0 &&
637-
*elem_size_bits % config.ansi_c.char_width == 0 &&
636+
array_type.size().is_constant() && elem_size_bits.has_value() &&
637+
*elem_size_bits > 0 && *elem_size_bits % config.ansi_c.char_width == 0 &&
638638
*target_size_bits <= *elem_size_bits)
639639
{
640+
const mp_integer array_size =
641+
numeric_cast_v<mp_integer>(to_constant_expr(array_type.size()));
640642
const mp_integer elem_size_bytes =
641643
*elem_size_bits / config.ansi_c.char_width;
644+
const mp_integer index = offset_bytes / elem_size_bytes;
642645
const auto offset_inside_elem = offset_bytes % elem_size_bytes;
643646
const auto target_size_bytes =
644647
*target_size_bits / config.ansi_c.char_width;
645648
// only recurse if the cell completely contains the target
646-
if(offset_inside_elem + target_size_bytes <= elem_size_bytes)
649+
if(
650+
index < array_size &&
651+
offset_inside_elem + target_size_bytes <= elem_size_bytes)
647652
{
648653
return get_subexpression_at_offset(
649654
index_exprt(

0 commit comments

Comments
 (0)