commit 11236c88e689f884619332bedbffd3501916f887
parent 2c35688619f6c5804106dfe52ee199894eb6b056
Author: Xavier Del Campo Romero <xavi92@disroot.org>
Date: Fri, 25 Apr 2025 10:27:33 +0200
cc1: Initialize correctly unions
When an union has an initializer then it initializes the first
member of the union unless a designated initializer is used.
The code was not ready to initialize an union with an implicit
value.
Diffstat:
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/cmd/scc-cc/cc1/code.c b/src/cmd/scc-cc/cc1/code.c
@@ -391,7 +391,8 @@ emitdesig(Node *np, Type *tp, SIZET *addr)
break;
case UNION:
aux = (sym) ? sym->u.init[0] : NULL;
- emitdesig(aux, aux->type, addr);
+ p = (aux) ? aux->type : tp->p.fields[0]->type;
+ emitdesig(aux, p, addr);
emitpadding(tp, addr);
break;
case STRUCT:
diff --git a/tests/cc/execute/0230-init.c b/tests/cc/execute/0230-init.c
@@ -0,0 +1,15 @@
+struct a {
+ int a;
+ union b {
+ int a;
+ char b;
+ } u;
+};
+
+int
+main()
+{
+ struct a a = {0};
+
+ return a.u.a;
+}
diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst
@@ -220,3 +220,4 @@
0227-sizeof.c
0228-extrcast.c
0229-commalog.c
+0230-init.c