[hackers] [scc] Fix size of composed types || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 25 Aug 2015 22:44:41 +0200 (CEST)

commit 5333c3cfde5f199f893cf52c4476f173b672bba0
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Aug 25 22:38:30 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Aug 25 22:38:30 2015 +0200

    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.

diff --git a/cc1/types.c b/cc1/types.c
index 2b2156e..c79a8f9 100644
--- a/cc1/types.c
+++ b/cc1/types.c
_AT_@ -261,6 +261,46 @@ invalid_type:
         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[])
 {
_AT_@ -321,6 +361,7 @@ mktype(Type *tp, unsigned op, short nelem, Type *pars[])
                 }
         }
 
+ type.size = typesize(&type);
         bp = duptype(&type);
         bp->next = *tbl;
         return *tbl = bp;
Received on Tue Aug 25 2015 - 22:44:41 CEST

This archive was generated by hypermail 2.3.0 : Tue Aug 25 2015 - 22:48:12 CEST