Skip to content

Commit 658b178

Browse files
marek-trtiktautschnig
authored andcommitted
Fixing member offset computation in presence of bitfields
1 parent a695814 commit 658b178

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

regression/cbmc/Bitfields3/main.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdlib.h>
2+
3+
struct A {
4+
unsigned char a;
5+
unsigned char b:2;
6+
unsigned char c:2;
7+
} __attribute__((packed));
8+
9+
int main(void)
10+
{
11+
struct A *p;
12+
p = malloc(2);
13+
p->c = 3;
14+
if (p->c != 3) {
15+
free(p);
16+
}
17+
free(p);
18+
}

regression/cbmc/Bitfields3/test.desc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/util/pointer_offset_size.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,15 @@ exprt member_offset_expr(
287287
if(it->type().id()==ID_c_bit_field)
288288
{
289289
std::size_t w=to_c_bit_field_type(it->type()).get_width();
290+
bit_field_bits += w;
290291
std::size_t bytes;
291-
for(bytes=0; w>bit_field_bits; ++bytes, bit_field_bits+=8) {}
292-
bit_field_bits-=w;
292+
bytes = bit_field_bits / 8;
293+
bit_field_bits = bit_field_bits % 8;
293294
result=plus_exprt(result, from_integer(bytes, result.type()));
294295
}
295296
else
296297
{
298+
bit_field_bits = 0;
297299
const typet &subtype=it->type();
298300
exprt sub_size=size_of_expr(subtype, ns);
299301
if(sub_size.is_nil())

0 commit comments

Comments
 (0)