scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 03dbb2f6ade20e9de9f3128f92c5a5496cf4183b
parent 438fb6e08b28ac019fb017c5caf1967ef51d0e1f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon,  7 Oct 2024 16:40:58 +0200

cc1: Don't use strlen concatenating strings

Adjacents strings must be concatenated, but the process was assuming
strings without embeddeding NUL characters (using strlen), but this
is not true as they can be embedded using character escape sequences.
For that reason we have to use the number of elements defined in the
type associated to the string constant.

Diffstat:
Msrc/cmd/cc/cc1/expr.c | 11++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c @@ -656,17 +656,18 @@ adjstrings(Symbol *sym) tp = sym->type; s = sym->u.s; - for (len = strlen(s);; len += n) { + for (len = tp->n.elem;; len += n) { next(); if (yytoken != STRING) break; t = yylval.sym->u.s; - n = strlen(t); - s = xrealloc(s, len + n + 1); - memcpy(s+len, t, n); + n = yylval.sym->type->n.elem - 1; + + s = xrealloc(s, len + n); + memcpy(s + len - 1, t, n); s[len + n] = '\0'; } - ++len; + if (tp->n.elem != len) { sym->type = mktype(chartype, ARY, len, NULL); sym->u.s = s;