From 487b71f82c9c56c8118b97b2b46e1397c7ae24ec Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 23 Sep 2025 23:57:15 +0200 Subject: [PATCH] ensure Addfile filehandle will always be properly closed TODO: probs could reuse this function in more places where filehandles are used --- src/d_netcmd.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 816548f49..9b6c0010a 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -4386,6 +4386,14 @@ static void Command_Addfilelocal(void) P_AddWadFile(fn, true); } +static void CloseFileHandle(FILE **fhandle) +{ + if (*fhandle) + { + fclose(*fhandle); + *fhandle = NULL; + } +} /** Adds a pwad at runtime. * Searches for sounds, maps, music, new images. @@ -4398,7 +4406,7 @@ static void Command_Addfile(void) INT32 i; int musiconly = -1; // W_VerifyNMUSlumps isn't boolean - FILE *fhandle = NULL; + CLEANUP(CloseFileHandle) FILE *fhandle = NULL; if (COM_Argc() != 2) { @@ -4418,7 +4426,7 @@ static void Command_Addfile(void) musiconly = W_VerifyNMUSlumps(fn, fhandle, false); } - if (musiconly == -1) + if (fhandle == NULL || musiconly == -1) return; // file not found if (!musiconly) @@ -4457,7 +4465,6 @@ static void Command_Addfile(void) CONS_Debug(DBG_SETUP, "Making MD5 for %s\n",fn); md5_stream(fhandle, md5sum); CONS_Debug(DBG_SETUP, "MD5 calc for %s took %f second\n", fn, (float)(I_GetTime() - t)/TICRATE); - fclose(fhandle); } for (i = 0; i < numwadfiles; i++)