Skip to content

Commit 6ef66d0

Browse files
committed
Packing with n>1 must be considered for nested structs
1 parent bf03089 commit 6ef66d0

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define STATIC_ASSERT(condition) \
2+
int some_array##__LINE__[(condition) ? 1 : -1]
3+
4+
#pragma pack(4)
5+
6+
typedef unsigned short domid_t;
7+
typedef unsigned evtchn_port_t;
8+
struct evtchn_status {
9+
domid_t dom;
10+
evtchn_port_t port;
11+
unsigned status;
12+
unsigned vcpu;
13+
union {
14+
struct {
15+
domid_t dom;
16+
} unbound;
17+
struct {
18+
domid_t dom;
19+
evtchn_port_t port;
20+
} interdomain;
21+
unsigned pirq;
22+
unsigned virq;
23+
} u;
24+
char a;
25+
};
26+
27+
int main()
28+
{
29+
STATIC_ASSERT(__builtin_offsetof(struct evtchn_status,u.interdomain.port)==20);
30+
STATIC_ASSERT(sizeof(struct evtchn_status)==28);
31+
return 0;
32+
};
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+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

src/ansi-c/padding.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ void add_padding(struct_typet &type, const namespacet &ns)
188188
}
189189
}
190190

191-
// Is the struct packed?
192-
if(type.get_bool(ID_C_packed))
191+
// Is the struct packed, without any alignment specification?
192+
if(type.get_bool(ID_C_packed) &&
193+
type.find(ID_C_alignment).is_nil())
193194
return; // done
194195

195196
mp_integer offset=0;
@@ -291,6 +292,9 @@ void add_padding(struct_typet &type, const namespacet &ns)
291292
max_alignment=tmp_i;
292293
}
293294
}
295+
// Is the struct packed, without any alignment specification?
296+
else if(type.get_bool(ID_C_packed))
297+
return; // done
294298

295299
// There may be a need for 'end of struct' padding.
296300
// We use 'max_alignment'.

0 commit comments

Comments
 (0)