From c59564723c727f4e803ac3c3aaa7337a7d0fa39b Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 3 Jul 2024 13:04:11 +0200 Subject: [PATCH] Clamp lindex during vissprite light calculation Mitigates against negative or very large xscale values causing lindex to evaluate < 0, preventing a potential colormap indexing-related crash. --- src/r_things.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/r_things.c b/src/r_things.c index 372ddb964..a769960ea 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -954,7 +954,6 @@ static void R_DrawPrecipitationVisSprite(vissprite_t *vis) if (vis->x2 >= vid.width) vis->x2 = vid.width-1; -#define CLAMP(x, min_val, max_val) ((x) < (min_val) ? (min_val) : ((x) > (max_val) ? (max_val) : (x))) for (dc_x = vis->x1; dc_x <= vis->x2; dc_x++, frac += vis->xiscale) { texturecolumn = CLAMP(frac >> FRACBITS, 0, SHORT(patch->width) - 1); @@ -962,7 +961,6 @@ static void R_DrawPrecipitationVisSprite(vissprite_t *vis) R_DrawMaskedColumn(column); } -#undef CLAMP colfunc = basecolfunc; } @@ -1048,8 +1046,9 @@ static void R_SplitSprite(vissprite_t *sprite, mobj_t *thing) { lindex = FixedMul(sprite->xscale, LIGHTRESOLUTIONFIX)>>(LIGHTSCALESHIFT); - if (lindex >= MAXLIGHTSCALE) - lindex = MAXLIGHTSCALE-1; + // Mitigate against negative xscale and arithmetic overflow + lindex = CLAMP(lindex, 0, MAXLIGHTSCALE - 1); + newsprite->colormap = spritelights[lindex]; } } @@ -1725,8 +1724,8 @@ static void R_ProjectSprite(mobj_t *thing) // diminished light lindex = FixedMul(xscale, LIGHTRESOLUTIONFIX)>>(LIGHTSCALESHIFT); - if (lindex >= MAXLIGHTSCALE) - lindex = MAXLIGHTSCALE-1; + // Mitigate against negative xscale and arithmetic overflow + lindex = CLAMP(lindex, 0, MAXLIGHTSCALE - 1); vis->colormap = spritelights[lindex]; }