From 08af33c51f7e8b8bc1f71cc63e6a096db03455c4 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Fri, 23 Nov 2018 19:12:48 +0000
Subject: [PATCH] Add myargmalloc boolean to detect whether we allocated myargv
 ourselves or not, so we don't free myargv if we didn't

---
 src/m_argv.c         | 5 +++++
 src/m_argv.h         | 1 +
 src/sdl/i_system.c   | 3 ++-
 src/sdl12/i_system.c | 3 ++-
 src/win32/win_sys.c  | 3 ++-
 5 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/m_argv.c b/src/m_argv.c
index 905ac6ab8..5c5dc37cf 100644
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -25,6 +25,10 @@ INT32 myargc;
 */
 char **myargv;
 
+/** \brief did we alloc myargv ourselves?
+*/
+boolean myargmalloc = false;
+
 /**	\brief founded the parm
 */
 static INT32 found;
@@ -176,6 +180,7 @@ void M_FindResponseFile(void)
 				free(file);
 				I_Error("Not enough memory to read response file");
 			}
+			myargmalloc = true; // mark as having been allocated by us
 			memset(myargv, 0, sizeof (char *) * MAXARGVS);
 			myargv[0] = firstargv;
 
diff --git a/src/m_argv.h b/src/m_argv.h
index 46ef9a2cf..af346a2c1 100644
--- a/src/m_argv.h
+++ b/src/m_argv.h
@@ -19,6 +19,7 @@
 //
 extern INT32 myargc;
 extern char **myargv;
+extern boolean myargmalloc;
 
 // Returns the position of the given parameter in the arg list (0 if not found).
 INT32 M_CheckParm(const char *check);
diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c
index 869fd7d98..2b35ce8b8 100644
--- a/src/sdl/i_system.c
+++ b/src/sdl/i_system.c
@@ -2139,7 +2139,8 @@ void I_Quit(void)
 		printf("\r");
 		ShowEndTxt();
 	}
-	free(myargv); // Deallocate allocated memory
+	if (myargmalloc)
+		free(myargv); // Deallocate allocated memory
 death:
 	W_Shutdown();
 	exit(0);
diff --git a/src/sdl12/i_system.c b/src/sdl12/i_system.c
index 2be6b55f0..8729e5921 100644
--- a/src/sdl12/i_system.c
+++ b/src/sdl12/i_system.c
@@ -2301,7 +2301,8 @@ void I_Quit(void)
 		printf("\r");
 		ShowEndTxt();
 	}
-	free(myargv); // Deallocate allocated memory
+	if (myargmalloc)
+		free(myargv); // Deallocate allocated memory
 death:
 	W_Shutdown();
 #ifdef GP2X
diff --git a/src/win32/win_sys.c b/src/win32/win_sys.c
index 110eafb7a..77a21f7f3 100644
--- a/src/win32/win_sys.c
+++ b/src/win32/win_sys.c
@@ -771,7 +771,8 @@ void I_Quit(void)
 		ShowEndTxt(co);
 	}
 	fflush(stderr);
-	free(myargv); // Deallocate allocated memory
+	if (myargmalloc)
+		free(myargv); // Deallocate allocated memory
 	W_Shutdown();
 	exit(0);
 }
-- 
GitLab