commit e15ff2385b9a9cc5cd228a877b06850a9c1681c3
parent c4250239fb94b6620bd6ea56eaba653b23a74784
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sun, 27 Mar 2022 09:48:24 +0200
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.
Diffstat:
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c
@@ -200,7 +200,7 @@ static int
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 @@ readline(void)
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 // */
diff --git a/tests/cc/execute/0203-comment.c b/tests/cc/execute/0203-comment.c
@@ -0,0 +1,7 @@
+int
+main(void)
+{
+ if (sizeof("//") != 3)
+ return 1;
+ return 0;
+}
diff --git 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