aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2026-04-10 11:46:13 +0200
committerlemon <lsof@mailbox.org>2026-04-10 11:46:13 +0200
commit87824748c3223f300dae9a725f823da02bff89d9 (patch)
treefc0ced195367666bc79a863366df0ea31e607a63
parent010ed5bc8a6840602776ddd1a2d4b7f817b0c56e (diff)
c: fix bitfields in unions
-rw-r--r--src/c.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/c.c b/src/c.c
index 78815be..c60c71f 100644
--- a/src/c.c
+++ b/src/c.c
@@ -2062,7 +2062,7 @@ buildagg(CComp *cm, enum typetag tt, internstr name, int id)
if (decl.ty.t) {
uint align = typealign(decl.ty);
uint siz = tysize;
- uint off = bitftypesiz ? bitfbyteoff : isunion ? 0 : alignup(td.siz, align);
+ uint off = isunion ? 0 : (bitftypesiz ? bitfbyteoff : alignup(td.siz, align));
NamedField f = { decl.name, { decl.ty, off, bitsiz, bitoff, .qual = decl.qual }};
if (bitftypesiz && siz != bitftypesiz) while (f.f.bitoff + f.f.bitsiz > 8*siz) {
/* adjust bitfields narrower than container type */
@@ -2086,12 +2086,14 @@ buildagg(CComp *cm, enum typetag tt, internstr name, int id)
if (typedata[decl.ty.dat].flexi && !isunion)
error(&decl.span, "nested aggregate has flexible array member");
}
- if (isunion)
+ if (isunion) {
td.siz = td.siz < siz ? siz : td.siz;
- else
+ bitsiz = bitfbyteoff = bitoff = bitftypesiz = 0;
+ } else {
+ bitoff += bitsiz;
td.siz = off + siz;
+ }
td.align = td.align < align ? align : td.align;
- bitoff += bitsiz;
}
} while (st.more);
}