foxygit / doom Log in
commit 1e9330683a2e1999bb2661789726f0b092f7bf7c
Author:     Turo Lamminen <turol@iki.fi>
AuthorDate: Thu Oct 20 11:25:02 2022 +0300
Commit:     Turo Lamminen <turol@iki.fi>
CommitDate: Thu Oct 20 12:16:19 2022 +0300

    heretic: Fix signedness problem in P_DamageMobj
---
 src/heretic/p_inter.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/heretic/p_inter.c b/src/heretic/p_inter.c
index ac843d25..ad1f7585 100644
--- a/src/heretic/p_inter.c
+++ b/src/heretic/p_inter.c
@@ -1350,7 +1350,12 @@ void P_DamageMobj
         ang = R_PointToAngle2(inflictor->x, inflictor->y,
                               target->x, target->y);
         //thrust = damage*(FRACUNIT>>3)*100/target->info->mass;
-        thrust = damage * (FRACUNIT >> 3) * 150 / target->info->mass;
+        // We do this multiplication in unsigned because it might overflow
+        // and signed overflow is undefined behavior
+        // but then we must cast it back to signed for the division
+        // to match original behavior
+        // unsigned to signed cast is implementation defined behavior at worst
+        thrust = ((int) (damage * (FRACUNIT >> 3) * 150u)) / target->info->mass;
         // make fall forwards sometimes
         if ((damage < 40) && (damage > target->health)
             && (target->z - inflictor->z > 64 * FRACUNIT) && (P_Random() & 1))