Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

poor struct definition in ipfw (bitfield with enum base type) ?

1 view
Skip to first unread message

Luigi Rizzo

unread,
Nov 29, 2009, 8:16:43 PM11/29/09
to
While trying to compile ipfw (userland) with tcc, i hit an issue
on the following struct in ip_fw.h (I think I wrote it):

typedef struct _ipfw_insn { /* template for instructions */
enum ipfw_opcodes opcode:8;
u_int8_t len; /* number of 32-bit words */
u_int16_t arg1;
} ipfw_insn;

gcc correctly packs the structure in 4 bytes, however tcc
fails to realize that the enum fits in 8 bits, and uses
the base type (int, which is 32 bit) which results in
the structure using 8 bytes. You can imagine the results.

I wonder if the difference is a bug in 'tcc' or it is one of
those things that are 'implementation-defined' by the C99 standard
(googling around seems to suggest the latter).

grepping through the entire /usr/src shows only one instance of
the above construct (a bitfield using an enum as base type).

To be safe, I'd be inclined to change 'opcode' to uint8_t,
which is non ambiguous and generates the same code on gcc and tcc

cheers
luigi
_______________________________________________
freebsd...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-curre...@freebsd.org"

Julian Elischer

unread,
Nov 29, 2009, 8:11:17 PM11/29/09
to
Luigi Rizzo wrote:
> While trying to compile ipfw (userland) with tcc, i hit an issue
> on the following struct in ip_fw.h (I think I wrote it):
>
> typedef struct _ipfw_insn { /* template for instructions */
> enum ipfw_opcodes opcode:8;
> u_int8_t len; /* number of 32-bit words */
> u_int16_t arg1;
> } ipfw_insn;
>
> gcc correctly packs the structure in 4 bytes, however tcc
> fails to realize that the enum fits in 8 bits, and uses
> the base type (int, which is 32 bit) which results in
> the structure using 8 bytes. You can imagine the results.

I think from memory enums are only defined to be ints
and an 8 bit enum is a gcc extension.

0 new messages