rcfile: do not allow a regex for name, header, or magic to be empty

If the user really wants to match anything, ".*" should be used.

(This also stops nano looking at the rest of the line as soon as an
empty regular expression is encountered.  This may seem like poorer
feedback than before, but... I think that multiple error messages
per line are more confusing than helpful.)

This fixes https://savannah.gnu.org/bugs/?57942.

Bug existed since before version 2.2.0.
This commit is contained in:
Benno Schulenberg 2020-03-03 17:37:13 +01:00
parent 4dab491df2
commit 5eb7145939

View File

@ -557,6 +557,8 @@ char *parse_argument(char *ptr)
* null-terminate it, and return a pointer to the succeeding text. */ * null-terminate it, and return a pointer to the succeeding text. */
char *parse_next_regex(char *ptr) char *parse_next_regex(char *ptr)
{ {
char *starting_point = ptr;
if (*(ptr - 1) != '"') { if (*(ptr - 1) != '"') {
jot_error(N_("Regex strings must begin and end with a \" character")); jot_error(N_("Regex strings must begin and end with a \" character"));
return NULL; return NULL;
@ -573,6 +575,11 @@ char *parse_next_regex(char *ptr)
return NULL; return NULL;
} }
if (ptr == starting_point) {
jot_error(N_("Empty regex string"));
return NULL;
}
/* Null-terminate the regex and skip until the next non-blank. */ /* Null-terminate the regex and skip until the next non-blank. */
*ptr++ = '\0'; *ptr++ = '\0';
@ -1073,10 +1080,7 @@ void parse_rule(char *ptr, int rex_flags)
if (ptr == NULL) if (ptr == NULL)
return; return;
if (*regexstring == '\0') { {
jot_error(N_("Empty regex string"));
goodstart = FALSE;
} else {
newcolor = (colortype *)nmalloc(sizeof(colortype)); newcolor = (colortype *)nmalloc(sizeof(colortype));
goodstart = compile(regexstring, rex_flags, &newcolor->start); goodstart = compile(regexstring, rex_flags, &newcolor->start);
} }
@ -1114,11 +1118,6 @@ void parse_rule(char *ptr, int rex_flags)
if (ptr == NULL) if (ptr == NULL)
return; return;
if (*regexstring == '\0') {
jot_error(N_("Empty regex string"));
return;
}
/* If the start regex was invalid, the end regex cannot be saved. */ /* If the start regex was invalid, the end regex cannot be saved. */
if (!goodstart) if (!goodstart)
return; return;