From 5f339fc2a922aa00a94027e885c29173e43edac7 Mon Sep 17 00:00:00 2001
From: Sryder <sryder13@gmail.com>
Date: Sun, 23 Jun 2019 14:52:49 +0100
Subject: [PATCH] Don't overlap strncpy in WAD file load

---
 src/w_wad.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/w_wad.c b/src/w_wad.c
index 8d96449f1c..e18c5a0840 100644
--- a/src/w_wad.c
+++ b/src/w_wad.c
@@ -149,9 +149,15 @@ FILE *W_OpenWadFile(const char **filename, boolean useerrors)
 {
 	FILE *handle;
 
-	strncpy(filenamebuf, *filename, MAX_WADPATH);
-	filenamebuf[MAX_WADPATH - 1] = '\0';
-	*filename = filenamebuf;
+	// Officially, strncpy should not have overlapping buffers, since W_VerifyNMUSlumps is called after this, and it
+	// changes filename to point at filenamebuf, it would technically be doing that. I doubt any issue will occur since
+	// they point to the same location, but it's better to be safe and this is a simple change.
+	if (filenamebuf != *filename)
+	{
+		strncpy(filenamebuf, *filename, MAX_WADPATH);
+		filenamebuf[MAX_WADPATH - 1] = '\0';
+		*filename = filenamebuf;
+	}
 
 	// open wad file
 	if ((handle = fopen(*filename, "rb")) == NULL)
-- 
GitLab