ensure Addfile filehandle will always be properly closed

TODO: probs could reuse this function in more places where filehandles are used
This commit is contained in:
Alug committed 2025-09-24 00:00:54 +02:00
1 parent 2b1b4544f9
commit 487b71f82c
1 file changed
+10 -3
+10 -3
View File
@@ -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++)