scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 5333c3cfde5f199f893cf52c4476f173b672bba0
parent 84ce51804b0bef5931a83fb48a5dbafc1e793443
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 25 Aug 2015 22:38:30 +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.

Diffstat:
Mcc1/types.c | 41+++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+), 0 deletions(-)

diff --git a/cc1/types.c b/cc1/types.c @@ -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[]) { @@ -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;