shithub: scc

Download patch

ref: f0dc7b162e3c03dfd5628941f0d1048fa0fdfda9
parent: a763df064b21eb5e74e8fe723180f201a04a1382
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Mar 27 05:48:24 EDT 2022

cc1: Don't accept comments in strings

Strings can store characters that represent comments and it means
that the presence of string and character constants must be checked
at the very beginning of the text processing in the code.

--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -200,7 +200,7 @@
 readline(void)
 {
 	char *bp, *lim;
-	int c, peekc = 0;
+	int c, peekc = 0, delim = 0;
 
 	if (feof(input->fp)) {
 		input->flags |= IEOF;
@@ -214,7 +214,23 @@
 		peekc = 0;
 		if (c == '\n' || c == EOF)
 			break;
-		if (c != '/')
+		if (c == '\\') {
+			peekc = readchar();
+			if (peekc == '\n' || peekc == EOF)
+				continue;
+			if (bp == lim-2)
+				break;
+			*bp++ = c;
+			c = peekc;
+			peekc = 0;
+			continue;
+		}
+
+		if (delim && c == delim)
+			delim = 0;
+		else if (!delim && (c == '"' || c == '\''))
+			delim = c;
+		if (c != '/' || delim)
 			continue;
 
 		/* check for /* or // */
--- /dev/null
+++ b/tests/cc/execute/0203-comment.c
@@ -1,0 +1,7 @@
+int
+main(void)
+{
+	if (sizeof("//") != 3)
+		return 1;
+	return 0;
+}
--- a/tests/cc/execute/scc-tests.lst
+++ b/tests/cc/execute/scc-tests.lst
@@ -193,3 +193,4 @@
 0200-cpp.c [TODO]
 0201-cpp.c [TODO]
 0202-variadic.c [TODO]
+0203-comment.c