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.)
This commit is contained in:
Benno Schulenberg 2020-05-01 13:52:25 +02:00
parent b9deb883fa
commit 1bbf07b27e
2 changed files with 4 additions and 7 deletions

View File

@ -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. */

View File

@ -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)
/* Precalculate the data for any multiline coloring regexes. */
precalc_multicolorinfo();
have_palette = FALSE;
#endif
refresh_needed = TRUE;