From 3b1d440d5cb229ceece68b1ad9bc364aeb818928 Mon Sep 17 00:00:00 2001 From: tkchia Date: Tue, 2 Aug 2022 15:28:21 +0000 Subject: [PATCH] Fix cpp.ansi crash when stringifying missing macro argument Fixes https://github.com/davidgiven/ack/issues/238 --- lang/cem/cpp.ansi/replace.c | 50 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lang/cem/cpp.ansi/replace.c b/lang/cem/cpp.ansi/replace.c index 7dd3c5c20..ca9db7888 100644 --- a/lang/cem/cpp.ansi/replace.c +++ b/lang/cem/cpp.ansi/replace.c @@ -706,32 +706,36 @@ static char *stringify( assert(n != 0); p = args->a_rawvec[n-1]; add2repl(repl, '"'); - while (*p) { - if (is_wsp(*p)) { - if (!space) { - space = 1; - add2repl(repl, ' '); - } - p++; - continue; - } - space = 0; - if (!delim && (*p == '"' || *p == '\'')) - delim = *p; - else if (*p == delim && !backslash) - delim = 0; - backslash = *p == '\\'; - if (*p == '"' || (delim && *p == '\\')) - add2repl(repl, '\\'); - if (*p == TOKSEP || *p == NOEXPM) p++; - else add2repl(repl, *p++); + if (p) { + while (*p) { + if (is_wsp(*p)) { + if (!space) { + space = 1; + add2repl(repl, ' '); + } + p++; + continue; + } + space = 0; + + if (!delim && (*p == '"' || *p == '\'')) + delim = *p; + else if (*p == delim && !backslash) + delim = 0; + backslash = *p == '\\'; + if (*p == '"' || (delim && *p == '\\')) + add2repl(repl, '\\'); + if (*p == TOKSEP || *p == NOEXPM) p++; + else add2repl(repl, *p++); + } + + /* trim spaces in the replacement list */ + for (--repl->r_ptr; is_wsp(*repl->r_ptr); repl->r_ptr--) + /* EMPTY */; + ++repl->r_ptr; /* oops, one to far */ } - /* trim spaces in the replacement list */ - for (--repl->r_ptr; is_wsp(*repl->r_ptr); repl->r_ptr--) - /* EMPTY */; - ++repl->r_ptr; /* oops, one to far */ add2repl(repl, '"'); } else error("illegal use of # operator");