From 59fb3ed900dd4e90b39b75cd7ba247180f94a662 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Sat, 10 Sep 2022 13:29:18 -0400 Subject: [PATCH 1/5] Attempt to fix use after free bug with precipitation mobjs on netgame load --- src/p_mobj.c | 28 +++++++++++++++++++++------- src/p_saveg.c | 8 +++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/p_mobj.c b/src/p_mobj.c index 530dd37dc..befcf04f9 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10298,20 +10298,34 @@ void P_RemovePrecipMobj(precipmobj_t *mobj) void P_RemoveSavegameMobj(mobj_t *mobj) { // unlink from sector and block lists - P_UnsetThingPosition(mobj); - - // Remove touching_sectorlist from mobj. - if (sector_list) + if (((thinker_t *)mobj)->function.acp1 == (actionf_p1)P_NullPrecipThinker) { - P_DelSeclist(sector_list); - sector_list = NULL; + P_UnsetPrecipThingPosition((precipmobj_t *)mobj); + + if (precipsector_list) + { + P_DelPrecipSeclist(precipsector_list); + precipsector_list = NULL; + } + } + else + { + // unlink from sector and block lists + P_UnsetThingPosition(mobj); + + // Remove touching_sectorlist from mobj. + if (sector_list) + { + P_DelSeclist(sector_list); + sector_list = NULL; + } } // stop any playing sound S_StopSound(mobj); // free block - P_RemoveThinker((thinker_t *)mobj); + P_RemoveThinkerDelayed((thinker_t *)mobj); // Call directly here since we are calling P_InitThinkers R_RemoveMobjInterpolator(mobj); } diff --git a/src/p_saveg.c b/src/p_saveg.c index f79b3f576..c3455498b 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -2166,6 +2166,12 @@ static void LoadMobjThinker(actionf_p1 thinker) mobj->player->viewz = mobj->player->mo->z + mobj->player->viewheight; } + if (mobj->type == MT_SKYBOX) + if (mobj->spawnpoint->options & MTF_OBJECTSPECIAL) + skyboxmo[1] = mobj; + else + skyboxmo[0] = mobj; + P_AddThinker(&mobj->thinker); if (diff2 & MD2_WAYPOINTCAP) @@ -2666,7 +2672,7 @@ static void P_NetUnArchiveThinkers(void) { next = currentthinker->next; - if (currentthinker->function.acp1 == (actionf_p1)P_MobjThinker) + if (currentthinker->function.acp1 == (actionf_p1)P_MobjThinker || currentthinker->function.acp1 == (actionf_p1)P_NullPrecipThinker) P_RemoveSavegameMobj((mobj_t *)currentthinker); // item isn't saved, don't remove it else Z_Free(currentthinker); From 3d311eeae3f3404bf4911d68f9aefe9d4da81544 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Sat, 10 Sep 2022 13:30:24 -0400 Subject: [PATCH 2/5] Fix compiler warning --- src/p_saveg.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/p_saveg.c b/src/p_saveg.c index c3455498b..a2656a467 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -2167,10 +2167,12 @@ static void LoadMobjThinker(actionf_p1 thinker) } if (mobj->type == MT_SKYBOX) + { if (mobj->spawnpoint->options & MTF_OBJECTSPECIAL) skyboxmo[1] = mobj; else skyboxmo[0] = mobj; + } P_AddThinker(&mobj->thinker); From c24a0d0a47c92670fe51fcb08fab7b50a55e0966 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Thu, 29 Sep 2022 15:43:50 -0400 Subject: [PATCH 3/5] Remove usage of currentthinker from direct removal It's designed to be referenced from P_RunTHinkers, whjich we aren't doing --- src/p_mobj.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/p_mobj.c b/src/p_mobj.c index befcf04f9..b2a9b09b5 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10323,10 +10323,17 @@ void P_RemoveSavegameMobj(mobj_t *mobj) // stop any playing sound S_StopSound(mobj); + R_RemoveMobjInterpolator(mobj); // free block - P_RemoveThinkerDelayed((thinker_t *)mobj); // Call directly here since we are calling P_InitThinkers - R_RemoveMobjInterpolator(mobj); + // Here we use the same code as R_RemoveThinkerDelayed, but without reference counting (we're removing everything so it shouldn't matter) and without touching currentthinker since we aren't in P_RunThinkers + { + thinker_t *thinker = (thinker_t *)mobj; + thinker_t *next = thinker->next; + (next->prev = thinker->prev)->next = next; + R_DestroyLevelInterpolators(thinker); + Z_Free(thinker); + } } static CV_PossibleValue_t respawnitemtime_cons_t[] = {{1, "MIN"}, {300, "MAX"}, {0, NULL}}; From f8d71450e5e6649e36f82448e92626b3bed713fb Mon Sep 17 00:00:00 2001 From: Ashnal Date: Thu, 29 Sep 2022 19:22:53 -0400 Subject: [PATCH 4/5] Unlink non-mobj and non-precip thinkers when loading and freeing Move globalweather to before P_SpawnSpecials so that specials can properly change weather and have it communicated in savegames --- src/p_mobj.c | 1 - src/p_saveg.c | 6 ++++++ src/p_setup.c | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/p_mobj.c b/src/p_mobj.c index b2a9b09b5..148004819 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10331,7 +10331,6 @@ void P_RemoveSavegameMobj(mobj_t *mobj) thinker_t *thinker = (thinker_t *)mobj; thinker_t *next = thinker->next; (next->prev = thinker->prev)->next = next; - R_DestroyLevelInterpolators(thinker); Z_Free(thinker); } } diff --git a/src/p_saveg.c b/src/p_saveg.c index a2656a467..758060033 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -2677,7 +2677,11 @@ static void P_NetUnArchiveThinkers(void) if (currentthinker->function.acp1 == (actionf_p1)P_MobjThinker || currentthinker->function.acp1 == (actionf_p1)P_NullPrecipThinker) P_RemoveSavegameMobj((mobj_t *)currentthinker); // item isn't saved, don't remove it else + { + (next->prev = currentthinker->prev)->next = next; + R_DestroyLevelInterpolators(currentthinker); Z_Free(currentthinker); + } } // we don't want the removed mobjs to come back @@ -3090,6 +3094,7 @@ static inline void P_NetArchiveSpecials(void) // Sky number WRITEINT32(save_p, globallevelskynum); + CONS_Printf("globalweather write %d", globalweather); // Current global weather type WRITEUINT8(save_p, globalweather); @@ -3126,6 +3131,7 @@ static void P_NetUnArchiveSpecials(void) P_SetupLevelSky(j, true); globalweather = READUINT8(save_p); + CONS_Printf("globalweather read %d", globalweather); if (globalweather) { diff --git a/src/p_setup.c b/src/p_setup.c index 328a3ff34..4f4140dfc 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -3124,14 +3124,14 @@ boolean P_SetupLevel(boolean skipprecip) if (!playerstarts[numcoopstarts]) break; + globalweather = mapheaderinfo[gamemap-1]->weather; + // set up world state P_SpawnSpecials(fromnetsave); if (loadprecip) // ugly hack for P_NetUnArchiveMisc (and P_LoadNetGame) P_SpawnPrecipitation(); - globalweather = mapheaderinfo[gamemap-1]->weather; - #ifdef HWRENDER // not win32 only 19990829 by Kin if (rendermode != render_soft && rendermode != render_none) { From 78ad817cf1afc6b230927805c300d249c785ce78 Mon Sep 17 00:00:00 2001 From: Ashnal Date: Thu, 29 Sep 2022 19:26:42 -0400 Subject: [PATCH 5/5] Forgot to remove my debug prints --- src/p_saveg.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/p_saveg.c b/src/p_saveg.c index 758060033..e3bc51d09 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -3094,7 +3094,6 @@ static inline void P_NetArchiveSpecials(void) // Sky number WRITEINT32(save_p, globallevelskynum); - CONS_Printf("globalweather write %d", globalweather); // Current global weather type WRITEUINT8(save_p, globalweather); @@ -3131,7 +3130,6 @@ static void P_NetUnArchiveSpecials(void) P_SetupLevelSky(j, true); globalweather = READUINT8(save_p); - CONS_Printf("globalweather read %d", globalweather); if (globalweather) {