From d675a549de5b1e5b89408f27b5490798a6995c01 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 4 Mar 2020 16:08:34 +0100 Subject: [PATCH] rcfile: don't store a coloring rule before it is complete This really fixes https://savannah.gnu.org/bugs/?57950. --- src/rcfile.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/rcfile.c b/src/rcfile.c index 7bac6ce2..0e5c222e 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -1062,6 +1062,8 @@ void parse_rule(char *ptr, int rex_flags) } while (*ptr != '\0') { + regex_t *start_rgx = NULL, *end_rgx = NULL; + /* Intermediate storage for compiled regular expressions. */ colortype *newcolor = NULL; /* Container for a regex (or regex pair) and the color it paints. */ bool expectend = FALSE; @@ -1078,18 +1080,37 @@ void parse_rule(char *ptr, int rex_flags) if (ptr == NULL) return; + /* When the regex is invalid, abandon the rule. */ + if (!compile(regexstring, rex_flags, &start_rgx)) + return; + + if (expectend) { + if (strncmp(ptr, "end=", 4) != 0) { + jot_error(N_("\"start=\" requires a corresponding \"end=\"")); + regfree(start_rgx); + free(start_rgx); + return; + } + + regexstring = ptr + 5; + ptr = parse_next_regex(ptr + 5); + + /* When there is no valid end= regex, abandon the rule. */ + if (ptr == NULL || !compile(regexstring, rex_flags, &end_rgx)) { + regfree(start_rgx); + free(start_rgx); + return; + } + } + newcolor = (colortype *)nmalloc(sizeof(colortype)); - /* When the regex is invalid, abandon the rule. */ - if (!compile(regexstring, rex_flags, &newcolor->start)) { - free(newcolor); - return; - } + newcolor->start = start_rgx; + newcolor->end = end_rgx; newcolor->fg = fg; newcolor->bg = bg; newcolor->attributes = attributes; - newcolor->end = NULL; if (lastcolor == NULL) live_syntax->color = newcolor; @@ -1102,24 +1123,6 @@ void parse_rule(char *ptr, int rex_flags) if (!expectend) continue; - if (strncmp(ptr, "end=", 4) != 0) { - jot_error(N_("\"start=\" requires a corresponding \"end=\"")); - return; - } - - regexstring = ptr + 5; - ptr = parse_next_regex(ptr + 5); - - if (ptr == NULL) - return; - - /* When the end= regex is invalid, abandon the whole rule. */ - if (!compile(regexstring, rex_flags, &newcolor->end)) { - regfree(newcolor->start); - free(newcolor); - return; - } - /* Lame way to skip another static counter. */ newcolor->id = live_syntax->nmultis; live_syntax->nmultis++;