ref: f95956fa753b147c2e78b1659f1c55ac8a0e50e6
parent: 7c8e979f1d5f58982cae51893eec769c28b716b3
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jan 20 10:55:00 EST 2016
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 ;))
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -99,6 +99,7 @@
Symbol *sym;
Type *btp;
size_t len;
+ char *s;
if ((tp->op == ARY || tp->op == STRUCT) &&
yytoken != '{' && yytoken != STRING) {
@@ -119,13 +120,16 @@
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;
}
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -230,9 +230,10 @@
{
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;
}