From 3c227ec532592ec91da874ff992a8d330016e10c Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 12 Jun 2026 22:06:47 +0200 Subject: [PATCH] P_CheckSight: fix rare overflow in rejectmatrix check didnt think this could overflow here but well it does wish there was a better way to bounds check heap arrays in C --- src/p_local.h | 8 +++++++- src/p_setup.c | 19 ++++++++++++------- src/p_sight.c | 21 ++++++++++++--------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/p_local.h b/src/p_local.h index 383469bcc..eb55f5dd4 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -414,7 +414,13 @@ boolean P_DoSpring(mobj_t *spring, mobj_t *object); // // P_SETUP // -extern UINT8 *rejectmatrix; // for fast sight rejection +typedef struct +{ + UINT8* data; + size_t size; +} reject_t; + +extern reject_t rejectmatrix; // for fast sight rejection extern INT32 *blockmaplump; // offsets in blockmap are from here extern INT32 *blockmap; // Big blockmap extern INT32 bmapwidth; diff --git a/src/p_setup.c b/src/p_setup.c index a1b1fb3cb..c8403538a 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -145,7 +145,7 @@ precipmobj_t **precipblocklinks = NULL; // Speeds up enemy AI by skipping detailed LineOf Sight calculation. // Without special effect, this could be used as a PVS lookup as well. // -UINT8 *rejectmatrix = NULL; +reject_t rejectmatrix = {}; // Maintain single and multi player starting spots. INT32 numdmstarts = 0, numcoopstarts = 0, numredctfstarts = 0, numbluectfstarts = 0; @@ -1307,7 +1307,7 @@ static void P_LoadSidedefs(void *data) if (msd->toptexture[0] == '-' && msd->toptexture[1] == '\0') break; else - memcpy(process,msd->toptexture,8); + memcpy(process, msd->toptexture, 8); if (msd->midtexture[0] != '-' || msd->midtexture[1] != '\0') memcpy(process+strlen(process), msd->midtexture, 8); if (msd->bottomtexture[0] != '-' || msd->bottomtexture[1] != '\0') @@ -1324,7 +1324,7 @@ static void P_LoadSidedefs(void *data) if (msd->toptexture[0] == '-' && msd->toptexture[1] == '\0') break; else - memcpy(process,msd->toptexture,8); + memcpy(process,msd->toptexture, 8); if (msd->midtexture[0] != '-' || msd->midtexture[1] != '\0') memcpy(process+strlen(process), msd->midtexture, 8); if (msd->bottomtexture[0] != '-' || msd->bottomtexture[1] != '\0') @@ -1787,14 +1787,16 @@ static void P_LoadReject(UINT8 *data, size_t count) { if (!count) // zero length, someone probably used ZDBSP { - rejectmatrix = NULL; + rejectmatrix.data = NULL; CONS_Debug(DBG_SETUP, "P_LoadReject: REJECT lump has size 0, will not be loaded\n"); } else { - rejectmatrix = Z_Malloc(count, PU_LEVEL, NULL); // allocate memory for the reject matrix - memcpy(rejectmatrix, data, count); // copy the data into it + rejectmatrix.data = Z_Malloc(count, PU_LEVEL, NULL); // allocate memory for the reject matrix + memcpy(rejectmatrix.data, data, count); // copy the data into it } + + rejectmatrix.size = count; } static void P_LoadMapBSP(const virtres_t* virt) @@ -1833,7 +1835,10 @@ static void P_LoadMapLUT(const virtres_t* virt) if (virtreject) P_LoadReject(virtreject->data, virtreject->size); else - rejectmatrix = NULL; + { + rejectmatrix.data = NULL; + rejectmatrix.size = 0; + } if (!(virtblockmap && P_LoadRawBlockMap(virtblockmap->data, virtblockmap->size))) P_CreateBlockMap(); diff --git a/src/p_sight.c b/src/p_sight.c index 896da76d6..63b87820e 100644 --- a/src/p_sight.c +++ b/src/p_sight.c @@ -31,8 +31,6 @@ typedef struct { fixed_t bbox[4]; } los_t; -static INT32 sightcounts[2]; - typedef INT32 (*divlinefunc)(fixed_t x, fixed_t y, const divline_t *node); typedef INT32 (*divlinecrossfunc)(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2, const divline_t *node); @@ -366,18 +364,25 @@ boolean P_CheckSight2(mobj_t *t1, mobj_t *t2, boolean fast) I_Assert(!P_MobjWasRemoved(t2)); if (!t1->subsector || !t2->subsector - || !t1->subsector->sector || !t2->subsector->sector) + || !t1->subsector->sector || !t2->subsector->sector) + { return false; + } s1 = t1->subsector->sector; s2 = t2->subsector->sector; pnum = (s1-sectors)*numsectors + (s2-sectors); - if (rejectmatrix != NULL) + if (rejectmatrix.data != NULL) { + const size_t shiftnum = pnum>>3; + // Check in REJECT table. - if (rejectmatrix[pnum>>3] & (1 << (pnum&7))) // can't possibly be connected + if (shiftnum < rejectmatrix.size && // check for overflow + (rejectmatrix.data[shiftnum] & (1 << (pnum&7)))) // can't possibly be connected + { return false; + } } // killough 11/98: shortcut for melee situations @@ -385,11 +390,9 @@ boolean P_CheckSight2(mobj_t *t1, mobj_t *t2, boolean fast) // haleyjd 02/23/06: can't do this if there are polyobjects in the subsec if (!t1->subsector->polyList && t1->subsector == t2->subsector) + { return true; - - // An unobstructed LOS is possible. - // Now look from eyes of t1 to any part of t2. - sightcounts[1]++; + } validcount++;