[hackers] [scc] Fill of 0 trailing space in string initialized || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Wed, 20 Jan 2016 16:02:24 +0100 (CET)

commit f95956fa753b147c2e78b1659f1c55ac8a0e50e6
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Jan 20 15:55:00 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Jan 20 15:55:00 2016 +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 ;))

diff --git a/cc1/init.c b/cc1/init.c
index 796cb0a..4fae85a 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -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) {
_AT_@ -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
index 534e4a0..1cdfa0f 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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;
 }
Received on Wed Jan 20 2016 - 16:02:24 CET

This archive was generated by hypermail 2.3.0 : Wed Jan 20 2016 - 16:12:31 CET