Avoid overflow in clipmove()

git-svn-id: https://svn.eduke32.com/eduke32@8633 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2020-02-11 09:21:28 +00:00 committed by Christoph Oelckers
parent 9269295102
commit 5a13be1f7b

View file

@ -1437,13 +1437,17 @@ int32_t clipmove(vec3_t * const pos, int16_t * const sectnum, int32_t xvect, int
if ((hitwall = cliptrace(pos->vec2, &vec)) >= 0) if ((hitwall = cliptrace(pos->vec2, &vec)) >= 0)
{ {
vec2_t const clipr = { clipit[hitwall].x2 - clipit[hitwall].x1, clipit[hitwall].y2 - clipit[hitwall].y1 }; vec2_t const clipr = { clipit[hitwall].x2 - clipit[hitwall].x1, clipit[hitwall].y2 - clipit[hitwall].y1 };
int64_t const templl = compat_maybe_truncate_to_int32((int64_t)clipr.x * clipr.x + (int64_t)clipr.y * clipr.y); // clamp to the max value we can utilize without reworking the scaling below
// this works around the overflow issue that affects dukedc2.map
int32_t const templl = (int32_t)clamp(compat_maybe_truncate_to_int32((int64_t)clipr.x * clipr.x + (int64_t)clipr.y * clipr.y), INT32_MIN, INT32_MAX);
if (templl > 0 && templl <= INT32_MAX) // without the upper bounds check this code misbehaves and occasionally makes the player move backwards. if (templl > 0 && templl <= INT32_MAX) // without the upper bounds check this code misbehaves and occasionally makes the player move backwards.
// This hints at another overflow problem elsewhere... // This hints at another overflow problem elsewhere...
{ {
int64_t const templl2 = compat_maybe_truncate_to_int32((int64_t)(goal.x-vec.x)*clipr.x + (int64_t)(goal.y-vec.y)*clipr.y); // I don't know if this one actually overflows or not, but I highly doubt it hurts to check
int32_t const i = (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829 || (llabs(templl2)>>11) < templl) ? int32_t const templl2
= (int32_t)clamp(compat_maybe_truncate_to_int32((int64_t)(goal.x - vec.x) * clipr.x + (int64_t)(goal.y - vec.y) * clipr.y), INT32_MIN, INT32_MAX);
int32_t const i = (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829 || (klabs(templl2)>>11) < templl) ?
divscale64(templl2, templl, 20) : 0; divscale64(templl2, templl, 20) : 0;
goal = { mulscale20(clipr.x, i)+vec.x, mulscale20(clipr.y, i)+vec.y }; goal = { mulscale20(clipr.x, i)+vec.x, mulscale20(clipr.y, i)+vec.y };