From f1fea30045845df5092e12b2d20bdf4b1fa93f08 Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Thu, 20 Dec 2018 17:56:51 -0500
Subject: [PATCH] Properly restrict EXECVERSION from cvar updates

---
 src/command.c | 15 ++++++++++++++-
 src/command.h |  1 +
 src/doomdef.h |  6 +++---
 src/m_misc.c  |  8 ++++----
 4 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/command.c b/src/command.c
index 3bd80e1ee..c7ce2bd3c 100644
--- a/src/command.c
+++ b/src/command.c
@@ -49,6 +49,7 @@ static void COM_Wait_f(void);
 static void COM_Help_f(void);
 static void COM_Toggle_f(void);
 
+static void CV_EnforceExecVersion(void);
 static boolean CV_FilterVarByVersion(consvar_t *v, const char *valstr);
 static boolean CV_Command(void);
 static consvar_t *CV_FindVar(const char *name);
@@ -66,7 +67,8 @@ CV_PossibleValue_t CV_Natural[] = {{1, "MIN"}, {999999999, "MAX"}, {0, NULL}};
 // Filter consvars by EXECVERSION
 // First implementation is 26 (2.1.21), so earlier configs default at 25 (2.1.20)
 // Also set CV_HIDEN during runtime, after config is loaded
-consvar_t cv_execversion = {"execversion","25",0,CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL};
+static boolean execversion_enabled = false;
+consvar_t cv_execversion = {"execversion","25",CV_CALL,CV_Unsigned, CV_EnforceExecVersion, 0, NULL, NULL, 0, 0, NULL};
 
 // for default joyaxis detection
 static boolean joyaxis_default = false;
@@ -1586,6 +1588,17 @@ void CV_InitFilterVar(void)
 	joyaxis_count = joyaxis2_count = 0;
 }
 
+void CV_ToggleExecVersion(boolean enable)
+{
+	execversion_enabled = enable;
+}
+
+static void CV_EnforceExecVersion(void)
+{
+	if (!execversion_enabled)
+		CV_StealthSetValue(&cv_execversion, EXECVERSION);
+}
+
 static boolean CV_FilterJoyAxisVars(consvar_t *v, const char *valstr)
 {
 	// If ALL axis settings are previous defaults, set them to the new defaults
diff --git a/src/command.h b/src/command.h
index 8dee1174c..e6767825c 100644
--- a/src/command.h
+++ b/src/command.h
@@ -130,6 +130,7 @@ extern CV_PossibleValue_t CV_Natural[];
 extern consvar_t cv_execversion;
 
 void CV_InitFilterVar(void);
+void CV_ToggleExecVersion(boolean enable);
 
 // register a variable for use at the console
 void CV_RegisterVar(consvar_t *variable);
diff --git a/src/doomdef.h b/src/doomdef.h
index 02b156d60..1cbe97f2a 100644
--- a/src/doomdef.h
+++ b/src/doomdef.h
@@ -217,9 +217,9 @@ extern FILE *logstream;
 #define MODVERSION 27
 
 // To version config.cfg, MAJOREXECVERSION is set equal to MODVERSION automatically.
-// Increment SUBEXECVERSION whenever a config change is needed
-// that does not correspond to an increment in MODVERSION.
-// If MAJOREXECVERSION increases, set MINOREXECVERSION to 0.
+// Increment MINOREXECVERSION whenever a config change is needed that does not correspond
+// to an increment in MODVERSION. This might never happen in practice.
+// If MODVERSION increases, set MINOREXECVERSION to 0.
 #define MAJOREXECVERSION MODVERSION
 #define MINOREXECVERSION 0
 // (It would have been nice to use VERSION and SUBVERSION but those are zero'd out for DEVELOP builds)
diff --git a/src/m_misc.c b/src/m_misc.c
index 1b92a8c4f..1ef74dc7b 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -443,7 +443,7 @@ void Command_LoadConfig_f(void)
 	FIL_ForceExtension(configfile, ".cfg");
 
 	// temporarily reset execversion to default
-	cv_execversion.flags = 0;
+	CV_ToggleExecVersion(true);
 	COM_BufInsertText(va("%s \"%s\"\n", cv_execversion.name, cv_execversion.defaultvalue));
 	CV_InitFilterVar();
 
@@ -452,7 +452,7 @@ void Command_LoadConfig_f(void)
 
 	// don't filter anymore vars and don't let this convsvar be changed
 	COM_BufInsertText(va("%s \"%d\"\n", cv_execversion.name, EXECVERSION));
-	cv_execversion.flags = CV_HIDEN;
+	CV_ToggleExecVersion(false);
 }
 
 /** Saves the current configuration and loads another.
@@ -494,7 +494,7 @@ void M_FirstLoadConfig(void)
 
 	// temporarily reset execversion to default
 	// we shouldn't need to do this, but JUST in case...
-	cv_execversion.flags = 0;
+	CV_ToggleExecVersion(true);
 	COM_BufInsertText(va("%s \"%s\"\n", cv_execversion.name, cv_execversion.defaultvalue));
 	CV_InitFilterVar();
 
@@ -504,7 +504,7 @@ void M_FirstLoadConfig(void)
 
 	// don't filter anymore vars and don't let this convsvar be changed
 	COM_BufInsertText(va("%s \"%d\"\n", cv_execversion.name, EXECVERSION));
-	cv_execversion.flags = CV_HIDEN;
+	CV_ToggleExecVersion(false);
 
 	// make sure I_Quit() will write back the correct config
 	// (do not write back the config if it crash before)