shithub: scc

Download patch

ref: 5333c3cfde5f199f893cf52c4476f173b672bba0
parent: 84ce51804b0bef5931a83fb48a5dbafc1e793443
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Aug 25 18:38:30 EDT 2015

Fix size of composed types

Size field of composed types was not initialized,
so any use of sizeof or addressing arrays was using
non initialized data.

--- a/cc1/types.c
+++ b/cc1/types.c
@@ -261,6 +261,46 @@
 	error("invalid type specification");
 }
 
+/* TODO: define a type for sizes instead of using short */
+static short
+typesize(Type *tp)
+{
+	short align, size;
+	Symbol **sp;
+	int n;
+
+	switch (tp->op) {
+	case ARY:
+		return tp->n.elem * tp->type->size;
+	case PTR:
+		return pvoidtype->size;
+	case STRUCT:
+		size = 0;
+		n = tp->n.elem;
+		for (sp = tp->p.fields; n--; ++sp) {
+			tp = (*sp)->type;
+			size += tp->size;
+			if (n > 0) {
+				align = tp->align - 1;
+				size += align - (size & align);
+			}
+		}
+		return size;
+	case UNION:
+		size = 0;
+		n = tp->n.elem;
+		for (sp = tp->p.fields; n--; ++sp) {
+			tp = (*sp)->type;
+			if (tp->size > size)
+				size = tp->size;
+		}
+		return size;
+	case ENUM:
+		return inttype->size;
+	}
+	return 0;
+}
+
 Type *
 mktype(Type *tp, unsigned op, short nelem, Type *pars[])
 {
@@ -321,6 +361,7 @@
 		}
 	}
 
+	type.size = typesize(&type);
 	bp = duptype(&type);
 	bp->next = *tbl;
 	return *tbl = bp;