scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 7e1565493b72f8aa6ab0270cd497a0e359374c2e
parent eed755976f16d60e0ea264cc7b374fa94c401028
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 26 Oct 2022 19:35:49 +0200

cc1: Don't discard cpp lines in moreinput()

The code in cpp is already discarding any input
so the write in input was not needed and dangerous
because as cpp() can call next() then input can
be null after coming back from cpp().

Diffstat:
Msrc/cmd/cc/cc1/cpp.c | 22++++++++++++++++++----
Msrc/cmd/cc/cc1/error.c | 3++-
Msrc/cmd/cc/cc1/lex.c | 4+---
3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c @@ -721,7 +721,6 @@ usererr(void) if (cppoff) return; cpperror("#error %s", input->p); - *input->p = '\0'; next(); } @@ -899,8 +898,11 @@ cpp(void) for (p = input->p; isspace(*p); ++p) ; - if (*p != '#') + if (*p != '#') { + if (cppoff) + *input->p = '\0'; return cppoff; + } input->p = p+1; disexpand = 1; @@ -932,8 +934,11 @@ cpp(void) * to skip this tests. For the same reason include() is the only * function which does not prepare the next token */ - if (yytoken != '\n' && !cppoff && bp->token != INCLUDE) - errorp("trailing characters after preprocessor directive"); + if (bp->token == INCLUDE) + goto ret; + + if (yytoken != '\n' && yytoken != EOFTOK && !cppoff) + cpperror("trailing characters after preprocessor directive"); ret: disescape = 0; @@ -941,6 +946,15 @@ ret: lexmode = CCMODE; namespace = ns; + /* + * at this point we know that the cpp line is processed, and any error + * is generated but as next is called we cannot be sure that input is + * valid anymore, but in case of begin valid we want to discard any + * pending input in the current line + */ + if (input) + *input->p = '\0'; + return 1; } diff --git a/src/cmd/cc/cc1/error.c b/src/cmd/cc/cc1/error.c @@ -72,7 +72,8 @@ cpperror(char *fmt, ...) va_end(va); /* discard input until the end of the line */ - *input->p = '\0'; + if (input) + *input->p = '\0'; next(); } diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c @@ -331,10 +331,8 @@ repeat: *input->p = '\0'; goto repeat; } - if (cpp()) { - *input->p = '\0'; + if (cpp()) goto repeat; - } } if (onlycpp && !wasexpand)