From 83c4dba4cee64f1bc1432f08cc5ba75aac822c24 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Thu, 2 Jun 2016 20:16:25 +0100
Subject: [PATCH] Fix crash reported by FuriousFox at
 http://mb.srb2.org/showthread.php?t=41536

Basically this makes sure numwadfiles is updated before loading the SOC/Lua scripts, so if a Lua script calls COM_BufInsertText with the contents "addfile scr_mysticrealm.wad" it can't overwrite the last written wadfile slot! Not that COM_BufInsertText really should be used like that to begin with
---
 src/w_wad.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/w_wad.c b/src/w_wad.c
index 40fea52230..aeaad3ced9 100644
--- a/src/w_wad.c
+++ b/src/w_wad.c
@@ -475,11 +475,11 @@ UINT16 W_LoadWadFile(const char *filename)
 	//
 	CONS_Printf(M_GetText("Added file %s (%u lumps)\n"), filename, numlumps);
 	wadfiles[numwadfiles] = wadfile;
-	W_LoadDehackedLumps(numwadfiles);
+	numwadfiles++; // must come BEFORE W_LoadDehackedLumps, so any addfile called by COM_BufInsertText called by Lua doesn't overwrite what we just loaded
+	W_LoadDehackedLumps(numwadfiles-1);
 
 	W_InvalidateLumpnumCache();
 
-	numwadfiles++;
 	return wadfile->numlumps;
 }
 
-- 
GitLab