From 82ad01cb432506409eb8a91f3dd19d5220ce8b71 Mon Sep 17 00:00:00 2001 From: Anonimus Date: Fri, 31 Oct 2025 11:55:16 -0400 Subject: [PATCH] Fix overflow in end-of-race DtF calcs --- src/k_kart.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/k_kart.c b/src/k_kart.c index 226949569..2008f37fb 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -8476,8 +8476,18 @@ static void K_UpdateDistanceFromFinishLine(player_t *player) const mapheader_t *mapheader = mapheaderinfo[gamemap - 1]; if ((mapheader->levelflags & LF_SECTIONRACE) == 0U) { - const UINT8 numfulllapsleft = ((UINT8)numlaps - player->laps) / mapheader->lapspersection; - player->distancetofinish += numfulllapsleft * K_GetCircuitLength(); + const INT16 numfulllapsleft = ((INT16)numlaps - (INT16)player->laps) / mapheader->lapspersection; + + if (numfulllapsleft >= 0) + { + player->distancetofinish += numfulllapsleft * K_GetCircuitLength(); + } + else + { + // Finished players don't really need to check for distance, do they? + // Lock in our position as a distance value. + player->distancetofinish = max(0, (INT16)player->position - 1); + } } } }