scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 8b96dd8a8d2e271d27d9407d4e847f1d3f194e13
parent aa770c65e75cb4c6d851368cc733fbdef6b0d94f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  3 Oct 2015 10:37:47 +0200

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()

Diffstat:
Mcc1/cpp.c | 23+++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -145,28 +145,39 @@ parsepars(char *buffer, char **listp, int nargs) 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)];