SDL_mixer: timidity (read_config_file): avoid rcf_count to constantly increase

From 18bfced56be73324b146b4c86f19df249065d10c Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Wed, 17 Mar 2021 23:56:50 +0300
Subject: [PATCH] timidity (read_config_file): avoid rcf_count to constantly
 increase

---
 src/codecs/timidity/timidity.c | 36 ++++++++++++++--------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/src/codecs/timidity/timidity.c b/src/codecs/timidity/timidity.c
index 23d3a1c..cddeabc 100644
--- a/src/codecs/timidity/timidity.c
+++ b/src/codecs/timidity/timidity.c
@@ -1,5 +1,4 @@
 /*
-
     TiMidity -- Experimental MIDI to WAVE converter
     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
 
@@ -26,6 +25,7 @@ static ToneBank *master_tonebank[MAXBANK], *master_drumset[MAXBANK];
 static char def_instr_name[256] = "";
 
 #define MAXWORDS 10
+#define MAX_RCFCOUNT 50
 
 /* Quick-and-dirty fgets() replacement. */
 
@@ -59,18 +59,16 @@ static char *RWgets(SDL_RWops *rw, char *s, int size)
     return (num_read != 0) ? s : NULL;
 }
 
-static int read_config_file(const char *name)
+static int read_config_file(const char *name, int rcf_count)
 {
   SDL_RWops *rw;
   char tmp[1024];
   char *w[MAXWORDS], *cp;
   char *endp;
   ToneBank *bank;
-  int i, j, k, line, words;
-  static int rcf_count=0;
+  int i, j, k, line, r, words;
 
-  if (rcf_count>50)
-  {
+  if (rcf_count >= MAX_RCFCOUNT) {
     SNDDBG(("Probable source loop in configuration files\n"));
     return -1;
   }
@@ -80,6 +78,7 @@ static int read_config_file(const char *name)
 
   bank = NULL;
   line = 0;
+  r = -1; /* start by assuming failure, */
 
   while (RWgets(rw, tmp, sizeof(tmp)))
   {
@@ -202,15 +201,11 @@ static int read_config_file(const char *name)
       }
       for (i=1; i<words; i++)
       {
-	int status;
-	rcf_count++;
-	status = read_config_file(w[i]);
-	rcf_count--;
-	if (status != 0) {
-	  SDL_RWclose(rw);
-	  return status;
-	}
+	r = read_config_file(w[i], rcf_count + 1);
+	if (r != 0)
+	  goto fail;
       }
+      r = -1; /* not finished yet, */
     }
     else if (!SDL_strcmp(w[0], "default"))
     {
@@ -285,10 +280,9 @@ static int read_config_file(const char *name)
 		name, line));
 	goto fail;
       }
-      if (bank->tone[i].name)
-	SDL_free(bank->tone[i].name);
+      SDL_free(bank->tone[i].name);
       sz = SDL_strlen(w[1])+1;
-      bank->tone[i].name=SDL_malloc(sz);
+      bank->tone[i].name = SDL_malloc(sz);
       SDL_memcpy(bank->tone[i].name,w[1],sz);
       bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
       bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
@@ -377,11 +371,11 @@ static int read_config_file(const char *name)
       }
     }
   }
-  SDL_RWclose(rw);
-  return 0;
+
+  r = 0; /* we're good. */
 fail:
   SDL_RWclose(rw);
-  return -2;
+  return r;
 }
 
 int Timidity_Init_NoConfig(void)
@@ -412,7 +406,7 @@ int Timidity_Init(const char *config_file)
   if (p != NULL)
     add_to_pathlist(config_file, p - config_file + 1);
 
-  if (read_config_file(config_file) < 0) {
+  if (read_config_file(config_file, 0) < 0) {
       Timidity_Exit();
       return -1;
   }