From 336ba6181e7750f62afbd7c2cc2da7ec33305643 Mon Sep 17 00:00:00 2001 From: smallmodel <15067410+smallmodel@users.noreply.github.com> Date: Mon, 5 Feb 2024 23:25:41 +0100 Subject: [PATCH] Don't allocate planes / facets if there are none --- code/qcommon/cm_patch.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/code/qcommon/cm_patch.c b/code/qcommon/cm_patch.c index ef4e31e6..34f367e9 100644 --- a/code/qcommon/cm_patch.c +++ b/code/qcommon/cm_patch.c @@ -1131,10 +1131,37 @@ static void CM_PatchCollideFromGrid( cGrid_t *grid, patchCollide_t *pf ) { // copy the results out pf->numPlanes = numPlanes; pf->numFacets = numFacets; - pf->facets = Hunk_Alloc(numFacets * sizeof(*pf->facets), h_dontcare); - Com_Memcpy(pf->facets, facets, numFacets * sizeof(*pf->facets)); - pf->planes = Hunk_Alloc(numPlanes * sizeof(*pf->planes), h_dontcare); - Com_Memcpy(pf->planes, planes, numPlanes * sizeof(*pf->planes)); + if (numPlanes >= 1) { + pf->facets = Hunk_Alloc(numFacets * sizeof(*pf->facets), h_dontcare); + Com_Memcpy(pf->facets, facets, numFacets * sizeof(*pf->facets)); + } else { + pf->numPlanes = 0; + pf->planes = NULL; + Com_DPrintf( + "WARNING: CM_PatchCollideFromGrid: Degenerate patch - no planes (%i %i %i)-(%i %i %i)\n", + pf->bounds[0][0], + pf->bounds[0][1], + pf->bounds[0][2], + pf->bounds[1][0], + pf->bounds[1][1], + pf->bounds[1][2]); + } + + if (numFacets >= 1) { + pf->planes = Hunk_Alloc(numPlanes * sizeof(*pf->planes), h_dontcare); + Com_Memcpy(pf->planes, planes, numPlanes * sizeof(*pf->planes)); + } else { + pf->numFacets = 0; + pf->facets = NULL; + Com_DPrintf( + "WARNING: CM_PatchCollideFromGrid: Degenerate patch - no facets (%i %i %i)-(%i %i %i)\n", + pf->bounds[0][0], + pf->bounds[0][1], + pf->bounds[0][2], + pf->bounds[1][0], + pf->bounds[1][1], + pf->bounds[1][2]); + } } @@ -1436,11 +1463,8 @@ void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s * //never clip against the back side if (hitnum == facet->numBorders - 1) continue; - if (enterFrac < leaveFrac && enterFrac >= 0) { + if (enterFrac <= leaveFrac && enterFrac >= 0) { if (enterFrac < tw->trace.fraction) { - if (enterFrac < 0) { - enterFrac = 0; - } #ifndef BSPC if (!cv) { cv = Cvar_Get( "r_debugSurfaceUpdate", "1", 0 );