foxygit / doom Log in
commit c181fa76b2a9f384a8e7d83b6180026a0538e01d
Author:     Julia Nechaevskaya <julia.nechaevskaya@live.com>
AuthorDate: Tue Feb 20 23:04:36 2024 +0300
Commit:     GitHub <noreply@github.com>
CommitDate: Tue Feb 20 21:04:36 2024 +0100

    Heretic: check if backsector is NULL (#1670)

    * Heretic: check if backsector is NULL

    * NULL pointer, not assumed random bytes

    * More correct comment

    * More actual comment

    Co-Authored-By: Turo Lamminen <turol@users.noreply.github.com>

    ---------

    Co-authored-by: Turo Lamminen <turol@users.noreply.github.com>
---
 src/heretic/p_map.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/heretic/p_map.c b/src/heretic/p_map.c
index ac516e15..ba566977 100644
--- a/src/heretic/p_map.c
+++ b/src/heretic/p_map.c
@@ -1199,14 +1199,21 @@ boolean PTR_AimTraverse(intercept_t * in)

         dist = FixedMul(attackrange, in->frac);

-        if (li->frontsector->floorheight != li->backsector->floorheight)
+        // Added checks if there is no backsector to prevent crashing.
+        // Crashes didn't happen in the DOS version of Heretic
+        // because reading NULL pointer produces unpredictable but
+        // deterministic values instead of crashing.
+        // See https://github.com/chocolate-doom/chocolate-doom/issues/1665
+        if (li->backsector == NULL
+            || li->frontsector->floorheight != li->backsector->floorheight)
         {
             slope = FixedDiv(openbottom - shootz, dist);
             if (slope > bottomslope)
                 bottomslope = slope;
         }

-        if (li->frontsector->ceilingheight != li->backsector->ceilingheight)
+        if (li->backsector == NULL
+            || li->frontsector->ceilingheight != li->backsector->ceilingheight)
         {
             slope = FixedDiv(opentop - shootz, dist);
             if (slope < topslope)