From 23d1d7e768e88dc9c8c511afcd2940bee562550a Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 24 Aug 2020 07:50:48 -0700 Subject: ffi: bugfix: zero-width bitfield offset problem. * ffi.c (make_ffi_type_struct): Fix incorrect condition for determining whether the zero-width bitfield allocates a unit or not. We must take into account the bit_offs, because it's possible that unit_offs and offs are the same, yet a previous bitfield has allocated some bits into the current allocation unit. For instance struct { char a : 1; uint32 : 0; char b } has size 5, but the equivalent FFI struct type ends up with size 1. After char a : 1, the byte offset is still zero, so if we don't look at the bit offset of 1, it looks like the allocation offset is aligned to the start of a uint32 cell, which then means that the zero-width bitfield is ignored. What's worse, the char b is then also allocated over top of the a : 1 bitfield. --- ffi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ffi.c') diff --git a/ffi.c b/ffi.c index dd2fe20d..d8522d89 100644 --- a/ffi.c +++ b/ffi.c @@ -3295,7 +3295,7 @@ static val make_ffi_type_struct(val syntax, val lisp_type, ucnum room = bits_type - bits_alloc; if (bits == 0) { - if (offs != unit_offs) + if (offs != unit_offs || bit_offs > 0) offs = unit_offs + size; bit_offs = 0; nmemb--, i--; -- cgit v1.2.3