shithub: scc

Download patch

ref: 5c7b4991a1e90baab76969d4778e8780e43d1239
parent: 0abc10c55a5366e6fdad4c775e5f18b39c14eec6
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Nov 13 02:55:10 EST 2021

Revert "cc1: Fix double free() error"

This reverts commit e8a346f902729546b0d5db1c6bf9f0325fd648c1.

--- a/src/cmd/cc/cc1/expr.c
+++ b/src/cmd/cc/cc1/expr.c
@@ -589,6 +589,34 @@
 	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 @@
 	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();
--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -544,42 +544,6 @@
 }
 
 /*
- * 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 @@
 
 	*bp++ = '"';
 	esc = 0;
-
-repeat:
 	for (++input->p; ; ++input->p) {
 		c = *input->p;
 
@@ -631,10 +593,6 @@
 	}
 
 	input->begin = ++input->p;
-
-	if (skipspaces() == '"')
-		goto repeat;
-
 	*bp = '\0';
 
 	yylen = bp - yytext + 1;
@@ -797,6 +755,42 @@
 }
 
 /* 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)