scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit f95956fa753b147c2e78b1659f1c55ac8a0e50e6
parent 7c8e979f1d5f58982cae51893eec769c28b716b3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 20 Jan 2016 15:55:00 +0100

Fill of 0 trailing space in string initialized

When a char array is initialized from a string with smaller size
then the trailing space must be filled with zero (someone said
that strncpy was not useful ;))

Diffstat:
cc1/init.c | 12++++++++----
cc1/symbol.c | 5+++--
2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/cc1/init.c b/cc1/init.c @@ -99,6 +99,7 @@ initialize(Type *tp) Symbol *sym; Type *btp; size_t len; + char *s; if ((tp->op == ARY || tp->op == STRUCT) && yytoken != '{' && yytoken != STRING) { @@ -119,13 +120,16 @@ initialize(Type *tp) len = strlen(sym->u.s); if (!tp->defined) { tp->defined = 1; - tp->n.elem = len; + tp->n.elem = len+1; } else if (tp->n.elem < len) { warn("initializer-string for array of chars is too long"); - sym = newstring(sym->u.s, tp->n.elem); - np->sym = sym; - np->type = sym->type; } + len = tp->n.elem; + s = sym->u.s; + sym = newstring(NULL, len); + strncpy(sym->u.s, s, len); + np->sym = sym; + np->type = sym->type; return np; } diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -230,9 +230,10 @@ newstring(char *s, size_t len) { Symbol *sym = newsym(NS_IDEN); - sym->flags |= ISSTRING | ISCONSTANT; + sym->flags |= ISSTRING | ISCONSTANT | ISPRIVATE; sym->u.s = xmalloc(len); - memcpy(sym->u.s, s, len); + if (s) + memcpy(sym->u.s, s, len); sym->type = mktype(chartype, ARY, len, NULL); return sym; }