Skip to content

Commit 30e72b7

Browse files
committed
Byte-extract lowering: do not blindly use offsets
When unpacking an array we must not create 0 bytes up until an offset when the source array can never be as large. Doing so when within a struct would spuriously produce a larger member. Fixes: #7654
1 parent 68af7a4 commit 30e72b7

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

regression/cbmc/byte_extract2/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
3+
#define SIZE 1
4+
5+
struct s
6+
{
7+
char a1[1];
8+
unsigned int a2_len;
9+
char a2[SIZE];
10+
};
11+
12+
void main()
13+
{
14+
struct s s1;
15+
char buf[SIZE];
16+
17+
__CPROVER_assume(s1.a2_len <= SIZE);
18+
__CPROVER_assume(s1.a2_len >= SIZE);
19+
char src_n[s1.a2_len];
20+
__CPROVER_array_copy(src_n, s1.a2);
21+
assert(src_n[0] == s1.a2[0]);
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring

src/util/lower_byte_operators.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,10 @@ static array_exprt unpack_struct(
777777
!bit_fields.has_value(),
778778
"all preceding members should have been processed");
779779

780-
exprt sub = unpack_rec(
781-
member,
782-
little_endian,
783-
offset_in_member,
784-
max_bytes_left,
785-
bits_per_byte,
786-
ns,
787-
true);
780+
// TODO: we could do better, perhaps checking whether this component needs
781+
// to be unpacked at all
782+
exprt sub =
783+
unpack_rec(member, little_endian, {}, {}, bits_per_byte, ns, true);
788784

789785
byte_operands.insert(
790786
byte_operands.end(),

0 commit comments

Comments
 (0)