From cc6437285fa3be85ca439d2477b9bd6702ec753a Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 13 Jun 2026 12:58:29 +0200 Subject: [PATCH] =?UTF-8?q?check=20for=20missized=20rejectmatrix=C2=B4s=20?= =?UTF-8?q?on=20load,=20pad=20them=20if=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this is what dsda-doom/prboom does ensures we wont crash from oob reads and makes us print a warning --- src/p_local.h | 8 +------- src/p_setup.c | 29 ++++++++++++++++++----------- src/p_sight.c | 7 ++----- src/y_inter.c | 2 +- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/p_local.h b/src/p_local.h index eb55f5dd4..383469bcc 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -414,13 +414,7 @@ boolean P_DoSpring(mobj_t *spring, mobj_t *object); // // P_SETUP // -typedef struct -{ - UINT8* data; - size_t size; -} reject_t; - -extern reject_t rejectmatrix; // for fast sight rejection +extern UINT8 *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 c8403538a..e8e654c3c 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. // -reject_t rejectmatrix = {}; +UINT8 *rejectmatrix = NULL; // Maintain single and multi player starting spots. INT32 numdmstarts = 0, numcoopstarts = 0, numredctfstarts = 0, numbluectfstarts = 0; @@ -1780,23 +1780,29 @@ static void P_GroupLines(void) } } - // PK3 version // -- Monster Iestyn 09/01/18 -static void P_LoadReject(UINT8 *data, size_t count) +static void P_LoadReject(UINT8 *data, size_t rejectsize) { - if (!count) // zero length, someone probably used ZDBSP + const size_t neededsize = (numsectors * numsectors + 7) >> 3; + size_t allocsize = rejectsize; + + if (!rejectsize) // zero length, someone probably used ZDBSP { - rejectmatrix.data = NULL; + rejectmatrix = NULL; CONS_Debug(DBG_SETUP, "P_LoadReject: REJECT lump has size 0, will not be loaded\n"); } else { - rejectmatrix.data = Z_Malloc(count, PU_LEVEL, NULL); // allocate memory for the reject matrix - memcpy(rejectmatrix.data, data, count); // copy the data into it - } + if (rejectsize < neededsize) + { + CONS_Alert(CONS_WARNING, "REJECT is %lu byte%s too small. REJECT might be invalid and might crash vanilla clients!\n", (neededsize - rejectsize), (neededsize - rejectsize) == 1 ? "" : "s"); + allocsize = neededsize; + } - rejectmatrix.size = count; + rejectmatrix = Z_Calloc(allocsize, PU_LEVEL, NULL); // allocate memory for the reject matrix + memcpy(rejectmatrix, data, rejectsize); // copy the data into it + } } static void P_LoadMapBSP(const virtres_t* virt) @@ -1833,11 +1839,12 @@ static void P_LoadMapLUT(const virtres_t* virt) // Lookup tables if (virtreject) + { P_LoadReject(virtreject->data, virtreject->size); + } else { - rejectmatrix.data = NULL; - rejectmatrix.size = 0; + rejectmatrix = NULL; } if (!(virtblockmap && P_LoadRawBlockMap(virtblockmap->data, virtblockmap->size))) diff --git a/src/p_sight.c b/src/p_sight.c index 63b87820e..7bde2d737 100644 --- a/src/p_sight.c +++ b/src/p_sight.c @@ -373,13 +373,10 @@ boolean P_CheckSight2(mobj_t *t1, mobj_t *t2, boolean fast) s2 = t2->subsector->sector; pnum = (s1-sectors)*numsectors + (s2-sectors); - if (rejectmatrix.data != NULL) + if (rejectmatrix != NULL) { - const size_t shiftnum = pnum>>3; - // Check in REJECT table. - if (shiftnum < rejectmatrix.size && // check for overflow - (rejectmatrix.data[shiftnum] & (1 << (pnum&7)))) // can't possibly be connected + if (rejectmatrix[pnum>>3] & (1 << (pnum&7))) // can't possibly be connected { return false; } diff --git a/src/y_inter.c b/src/y_inter.c index 19b696b0e..a92392f39 100644 --- a/src/y_inter.c +++ b/src/y_inter.c @@ -586,7 +586,7 @@ void Y_Ticker(void) if (data.rankingsmode && intertic > sorttic+16+(2*TICRATE)) { - INT32 q=0,r=0; + INT32 q = 0, r = 0; boolean kaching = true; for (q = 0; q < data.numplayers; q++)