Clean up instances of malloc replay file path

- Fix allocating only enough for "MAPXX".
- Fix memory leak in M_StartTimeAttack.
- Use va instead of malloc + sprintf, strdup where needed.
Some of these pass gpath to va anyway so that should be
enough room.
This commit is contained in:
James R 2022-10-13 16:32:07 -07:00
parent e7b493723f
commit 01faa52162
3 changed files with 11 additions and 16 deletions

View file

@ -516,7 +516,6 @@ tic_t G_GetBestLap(INT16 map)
// //
static void G_UpdateRecordReplays(void) static void G_UpdateRecordReplays(void)
{ {
const size_t glen = strlen(srb2home)+1+strlen("media")+strlen("replay")+1+strlen(timeattackfolder)+1+strlen("MAPXX")+1;
char *gpath; char *gpath;
char lastdemo[256], bestdemo[256]; char lastdemo[256], bestdemo[256];
UINT8 earnedEmblems; UINT8 earnedEmblems;
@ -556,12 +555,13 @@ static void G_UpdateRecordReplays(void)
srb2home, timeattackfolder); srb2home, timeattackfolder);
M_MkdirEach(gpath, M_PathParts(gpath) - 3, 0755); M_MkdirEach(gpath, M_PathParts(gpath) - 3, 0755);
if ((gpath = malloc(glen)) == NULL) strcat(gpath, PATHSEP);
I_Error("Out of memory for replay filepath\n"); strcat(gpath, G_BuildMapName(gamemap));
sprintf(gpath,"%s"PATHSEP"media"PATHSEP"replay"PATHSEP"%s"PATHSEP"%s", srb2home, timeattackfolder, G_BuildMapName(gamemap));
snprintf(lastdemo, 255, "%s-%s-last.lmp", gpath, cv_chooseskin.string); snprintf(lastdemo, 255, "%s-%s-last.lmp", gpath, cv_chooseskin.string);
gpath = Z_StrDup(gpath);
if (FIL_FileExists(lastdemo)) if (FIL_FileExists(lastdemo))
{ {
UINT8 *buf; UINT8 *buf;
@ -592,7 +592,8 @@ static void G_UpdateRecordReplays(void)
Z_Free(buf); Z_Free(buf);
} }
free(gpath);
Z_Free(gpath);
// Check emblems when level data is updated // Check emblems when level data is updated
if ((earnedEmblems = M_CheckLevelEmblems())) if ((earnedEmblems = M_CheckLevelEmblems()))

View file

@ -3670,7 +3670,6 @@ void M_SetGuestReplay(INT32 choice)
void M_StartTimeAttack(INT32 choice) void M_StartTimeAttack(INT32 choice)
{ {
char *gpath; char *gpath;
const size_t glen = strlen("media")+1+strlen("replay")+1+strlen(timeattackfolder)+1+strlen("MAPXX")+1;
char nameofdemo[256]; char nameofdemo[256];
(void)choice; (void)choice;
@ -3713,10 +3712,9 @@ void M_StartTimeAttack(INT32 choice)
srb2home, timeattackfolder); srb2home, timeattackfolder);
M_MkdirEach(gpath, M_PathParts(gpath) - 3, 0755); M_MkdirEach(gpath, M_PathParts(gpath) - 3, 0755);
if ((gpath = malloc(glen)) == NULL) strcat(gpath, PATHSEP);
I_Error("Out of memory for replay filepath\n"); strcat(gpath, G_BuildMapName(levellist.choosemap+1));
sprintf(gpath,"media"PATHSEP"replay"PATHSEP"%s"PATHSEP"%s", timeattackfolder, G_BuildMapName(levellist.choosemap+1));
snprintf(nameofdemo, sizeof nameofdemo, "%s-%s-last", gpath, cv_skin[0].string); snprintf(nameofdemo, sizeof nameofdemo, "%s-%s-last", gpath, cv_skin[0].string);
if (!cv_autorecord.value) if (!cv_autorecord.value)

View file

@ -3701,14 +3701,10 @@ static void P_ResetSpawnpoints(void)
static void P_LoadRecordGhosts(void) static void P_LoadRecordGhosts(void)
{ {
// see also k_menu.c's Nextmap_OnChange // see also k_menu.c's Nextmap_OnChange
const size_t glen = strlen(srb2home)+1+strlen("media")+1+strlen("replay")+1+strlen(timeattackfolder)+1+strlen("MAPXX")+1; char *gpath;
char *gpath = malloc(glen);
INT32 i; INT32 i;
if (!gpath) gpath = Z_StrDup(va("%s"PATHSEP"media"PATHSEP"replay"PATHSEP"%s"PATHSEP"%s", srb2home, timeattackfolder, G_BuildMapName(gamemap)));
return;
sprintf(gpath,"%s"PATHSEP"media"PATHSEP"replay"PATHSEP"%s"PATHSEP"%s", srb2home, timeattackfolder, G_BuildMapName(gamemap));
// Best Time ghost // Best Time ghost
if (cv_ghost_besttime.value) if (cv_ghost_besttime.value)
@ -3771,7 +3767,7 @@ static void P_LoadRecordGhosts(void)
} }
#endif //#ifdef STAFFGHOSTS #endif //#ifdef STAFFGHOSTS
free(gpath); Z_Free(gpath);
} }
static void P_SetupCamera(UINT8 pnum, camera_t *cam) static void P_SetupCamera(UINT8 pnum, camera_t *cam)