shithub: scc

Download patch

ref: 8b96dd8a8d2e271d27d9407d4e847f1d3f194e13
parent: aa770c65e75cb4c6d851368cc733fbdef6b0d94f
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Oct 3 06:37:47 EDT 2015

Protect @ and $ in strings when expand macros

@ and $ are used in the preprocessor to mark
arguments and concatenation, but they can
appear in strings, so we have to handle
strings in copymacro()

--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -145,28 +145,39 @@
 	return 1;
 }
 
+/* FIXME: characters in the definition break the macro definition */
 static size_t
 copymacro(char *buffer, char *s, size_t bufsiz, char *arglist[])
 {
-	char prevc, c, *arg, *bp = buffer;
+	char prevc, c, *p, *arg, *bp = buffer;
+	size_t size;
 
 	for (prevc = '\0'; c = *s; prevc = c, ++s) {
 		if (c != '@') {
-			if (c == '#')
-				continue;
-			if (c == '$') {
+			switch (c) {
+			case '$':
 				while (bp[-1] == ' ')
 					--bp, ++bufsiz;
 				while (s[1] == ' ')
 					++s;
+			case '#':
 				continue;
+			case '\"':
+				for (p = s; *++s != '"'; )
+					/* nothing */;
+				size = s - p + 1;
+				if (size > bufsiz)
+					goto expansion_too_long;
+				memcpy(bp, p, size);
+				bufsiz -= size;
+				bp += size;
+				continue;
+			// case '\'';
 			}
 			if (bufsiz-- == 0)
 				goto expansion_too_long;
 			*bp++ = c;
 		} else {
-			size_t size;
-
 			if (prevc == '#')
 				bufsiz -= 2;
 			arg = arglist[atoi(++s)];