From 275e9f0092c82839f5b798adbd7ece779a2a2201 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sun, 28 Feb 2016 20:38:14 +0000
Subject: [PATCH] Disallowing the addition of further things to a syntax when
 an rcfile ends or when an invalid syntax command is found. This fixes
 Savannah bug #47207

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5696 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  3 +++
 src/rcfile.c | 18 ++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9676b159..b40e7acb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,9 @@
 	* src/rcfile.c (grab_and_store): Do not drop regexes that were
 	gathered earlier.  This fixes Savannah bug #47285.
 	* src/rcfile.c (grab_and_store): Rearrange things in my style.
+	* src/rcfile.c (parse_syntax, parse_rcfile): Disallow adding any
+	further things to a syntax when an rcfile ends or when an invalid
+	syntax command is found.  This fixes Savannah bug #47207.
 
 2016-02-26  Benno Schulenberg  <bensberg@justemail.net>
 	* doc/man/nanorc.5, doc/texinfo/nano.texi, doc/syntax/nanorc.nanorc,
diff --git a/src/rcfile.c b/src/rcfile.c
index f8b19419..a020dcee 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -120,6 +120,9 @@ static size_t lineno = 0;
 static char *nanorc = NULL;
 	/* The path to the rcfile we're parsing. */
 #ifndef DISABLE_COLOR
+static bool opensyntax = FALSE;
+	/* Whether we're allowed to add to the last syntax.  When a file ends,
+	 * or when a new syntax command is seen, this bool becomes FALSE. */
 static syntaxtype *endsyntax = NULL;
 	/* The end of the list of syntaxes. */
 static colortype *endcolor = NULL;
@@ -268,6 +271,8 @@ void parse_syntax(char *ptr)
     regexlisttype *endext = NULL;
 	/* The end of the extensions list for this syntax. */
 
+    opensyntax = FALSE;
+
     assert(ptr != NULL);
 
     if (*ptr == '\0') {
@@ -335,6 +340,8 @@ void parse_syntax(char *ptr)
     endsyntax->linter = NULL;
     endsyntax->formatter = NULL;
 
+    opensyntax = TRUE;
+
 #ifdef DEBUG
     fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
 #endif
@@ -691,7 +698,7 @@ void parse_colors(char *ptr, bool icase)
 
     assert(ptr != NULL);
 
-    if (syntaxes == NULL) {
+    if (!opensyntax) {
 	rcfile_error(
 		N_("Cannot add a color command without a syntax command"));
 	return;
@@ -862,7 +869,7 @@ void grab_and_store(char *ptr, const char *kind, regexlisttype **storage)
 {
     regexlisttype *lastthing;
 
-    if (syntaxes == NULL) {
+    if (!opensyntax) {
 	rcfile_error(
 		N_("A '%s' command requires a preceding 'syntax' command"), kind);
 	return;
@@ -933,7 +940,7 @@ void parse_linter(char *ptr)
 {
     assert(ptr != NULL);
 
-    if (syntaxes == NULL) {
+    if (!opensyntax) {
 	rcfile_error(
 		N_("Cannot add a linter without a syntax command"));
 	return;
@@ -959,7 +966,7 @@ void parse_formatter(char *ptr)
 {
     assert(ptr != NULL);
 
-    if (syntaxes == NULL) {
+    if (!opensyntax) {
 	rcfile_error(
 		N_("Cannot add formatter without a syntax command"));
 	return;
@@ -1062,6 +1069,7 @@ void parse_rcfile(FILE *rcstream
 		rcfile_error(N_("Could not find syntax \"%s\" to extend"), syntaxname);
 		continue;
 	    } else {
+		opensyntax = TRUE;
 		end_syn_save = endsyntax;
 		endsyntax = ts;
 		keyword = ptr;
@@ -1313,6 +1321,8 @@ void parse_rcfile(FILE *rcstream
 		endsyntax->desc);
 #endif
 
+    opensyntax = FALSE;
+
     free(buf);
     fclose(rcstream);
     lineno = 0;
-- 
GitLab