commit 40c55f50ab9afe5b85dd2bd5450ee9056c00b4c6
parent e8a346f902729546b0d5db1c6bf9f0325fd648c1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 13 Nov 2021 07:55:10 +0100
Revert "cc1: Fix double free() error"
This reverts commit e8a346f902729546b0d5db1c6bf9f0325fd648c1.
Diffstat:
2 files changed, 66 insertions(+), 45 deletions(-)
diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c
@@ -589,6 +589,34 @@ notdefined(Symbol *sym)
return install(sym->ns, yylval.sym);
}
+static Symbol *
+adjstrings(Symbol *sym)
+{
+ char *s, *t;
+ size_t len, n;
+ Type *tp;
+
+ tp = sym->type;
+ s = sym->u.s;
+ for (len = strlen(s);; 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);
+ s[len + n] = '\0';
+ killsym(yylval.sym);
+ }
+ ++len;
+ if (tp->n.elem != len) {
+ sym->type = mktype(chartype, ARY, len, NULL);
+ sym->u.s = s;
+ }
+ return sym;
+}
+
/*************************************************************
* grammar functions *
*************************************************************/
@@ -602,12 +630,11 @@ primary(void)
sym = yylval.sym;
switch (yytoken) {
case STRING:
- np = constnode(sym);
+ np = constnode(adjstrings(sym));
sym->flags |= SHASINIT;
emit(ODECL, sym);
emit(OINIT, np);
- np = varnode(sym);
- break;
+ return varnode(sym);
case BUILTIN:
fun = sym->u.fun;
next();
diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c
@@ -544,42 +544,6 @@ character(void)
}
/*
- * skip all the spaces until the next token. When we are in
- * CPPMODE \n is not considered a whitespace
- */
-static int
-skipspaces(void)
-{
- int c;
-
- for (;;) {
- switch (c = *input->p) {
- case '\n':
- if (lexmode == CPPMODE)
- goto return_byte;
- ++input->p;
- case '\0':
- if (!moreinput())
- return EOF;
- break;
- case ' ':
- case '\t':
- case '\v':
- case '\r':
- case '\f':
- ++input->p;
- break;
- default:
- goto return_byte;
- }
- }
-
-return_byte:
- input->begin = input->p;
- return c;
-}
-
-/*
* string() parses a constant string, and convert all the
* escape sequences into single characters. This behaviour
* is correct except when we parse a #define, where we want
@@ -595,8 +559,6 @@ string(void)
*bp++ = '"';
esc = 0;
-
-repeat:
for (++input->p; ; ++input->p) {
c = *input->p;
@@ -631,10 +593,6 @@ repeat:
}
input->begin = ++input->p;
-
- if (skipspaces() == '"')
- goto repeat;
-
*bp = '\0';
yylen = bp - yytext + 1;
@@ -798,6 +756,42 @@ operator(void)
/* TODO: Ensure that namespace is NS_IDEN after a recovery */
+/*
+ * skip all the spaces until the next token. When we are in
+ * CPPMODE \n is not considered a whitespace
+ */
+static int
+skipspaces(void)
+{
+ int c;
+
+ for (;;) {
+ switch (c = *input->p) {
+ case '\n':
+ if (lexmode == CPPMODE)
+ goto return_byte;
+ ++input->p;
+ case '\0':
+ if (!moreinput())
+ return EOF;
+ break;
+ case ' ':
+ case '\t':
+ case '\v':
+ case '\r':
+ case '\f':
+ ++input->p;
+ break;
+ default:
+ goto return_byte;
+ }
+ }
+
+return_byte:
+ input->begin = input->p;
+ return c;
+}
+
int
next(void)
{