From 1bbf07b27e790fb224e629f9cf103974659e6885 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 1 May 2020 13:52:25 +0200 Subject: [PATCH] color: avoid allocating emptiness when there are no multiline regexes When a file is saved under a different name, and as a result the applicable syntax changes, and the old syntax had multiline regexes and the new syntax doesn't, then the call of precalc_multicolorinfo() in write_file() should not result in nano setting up a multicache of zero bytes for each line in the buffer. (Problem was found by locally letting nano crash when zero bytes are allocated, and then happening to rename a file.py to a file.sh.) --- src/color.c | 3 ++- src/files.c | 8 ++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/color.c b/src/color.c index 4abe2184..2a062fa3 100644 --- a/src/color.c +++ b/src/color.c @@ -315,7 +315,8 @@ void precalc_multicolorinfo(void) regmatch_t startmatch, endmatch; linestruct *line, *tailline; - if (openfile->syntax == NULL || ISSET(NO_SYNTAX)) + if (openfile->syntax == NULL || openfile->syntax->nmultis == 0 || + openfile->filetop->multidata || ISSET(NO_SYNTAX)) return; /* For each line, allocate cache space for the multiline-regex info. */ diff --git a/src/files.c b/src/files.c index 02795d91..50d63e01 100644 --- a/src/files.c +++ b/src/files.c @@ -497,12 +497,8 @@ void prepare_for_display(void) titlebar(NULL); #ifdef ENABLE_COLOR - /* If there are multiline coloring regexes, and there is no - * multiline cache data yet, precalculate it now. */ - if (openfile->syntax && openfile->syntax->nmultis > 0 && - openfile->filetop->multidata == NULL) - precalc_multicolorinfo(); - + /* Precalculate the data for any multiline coloring regexes. */ + precalc_multicolorinfo(); have_palette = FALSE; #endif refresh_needed = TRUE;