COM_LoadMallocFile_TextMode_OSPath: do not leak open file

This commit is contained in:
temx 2022-09-12 20:05:50 +03:00 committed by Ozkan Sezer
parent 4df8bb389a
commit ea4320d675

View file

@ -1875,7 +1875,7 @@ byte *COM_LoadMallocFile_TextMode_OSPath (const char *path, long *len_out)
FILE *f; FILE *f;
byte *data; byte *data;
long len, actuallen; long len, actuallen;
// ericw -- this is used by Host_Loadgame_f. Translate CRLF to LF on load games, // ericw -- this is used by Host_Loadgame_f. Translate CRLF to LF on load games,
// othewise multiline messages have a garbage character at the end of each line. // othewise multiline messages have a garbage character at the end of each line.
// TODO: could handle in a way that allows loading CRLF savegames on mac/linux // TODO: could handle in a way that allows loading CRLF savegames on mac/linux
@ -1883,26 +1883,34 @@ byte *COM_LoadMallocFile_TextMode_OSPath (const char *path, long *len_out)
f = fopen (path, "rt"); f = fopen (path, "rt");
if (f == NULL) if (f == NULL)
return NULL; return NULL;
len = COM_filelength (f); len = COM_filelength (f);
if (len < 0) if (len < 0)
{
fclose (f);
return NULL; return NULL;
}
data = (byte *) malloc (len + 1); data = (byte *) malloc (len + 1);
if (data == NULL) if (data == NULL)
{
fclose (f);
return NULL; return NULL;
}
// (actuallen < len) if CRLF to LF translation was performed // (actuallen < len) if CRLF to LF translation was performed
actuallen = fread (data, 1, len, f); actuallen = fread (data, 1, len, f);
if (ferror(f)) if (ferror(f))
{ {
fclose (f);
free (data); free (data);
return NULL; return NULL;
} }
data[actuallen] = '\0'; data[actuallen] = '\0';
if (len_out != NULL) if (len_out != NULL)
*len_out = actuallen; *len_out = actuallen;
fclose (f);
return data; return data;
} }