Skip to content

Commit

Permalink
Guarded against stackalloc-induced overflow for large compounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossNordby committed Jan 13, 2024
1 parent 563773c commit fe8d87e
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ public static unsafe void FindLocalOverlaps(ref Buffer<BoundsTestedPair> pairs,
}
overlaps = new CompoundPairOverlaps(pool, pairCount, totalCompoundChildCount);
ref var pairsToTest = ref overlaps.pairQueries;
var subpairData = stackalloc SubpairData[totalCompoundChildCount];
//Stack overflows are very possible with larger compounds! Guard against it.
Buffer<SubpairData> subpairData;
const int stackallocThreshold = 1024;
if (totalCompoundChildCount <= stackallocThreshold)
{
var memory = stackalloc SubpairData[totalCompoundChildCount];
subpairData = new Buffer<SubpairData>(memory, totalCompoundChildCount);
}
else
{
subpairData = new Buffer<SubpairData>(totalCompoundChildCount, pool);
}
int nextSubpairIndex = 0;
for (int i = 0; i < pairCount; ++i)
{
Expand Down Expand Up @@ -112,8 +123,10 @@ out GatherScatter.Get(ref maximumRadius, j),
}
//Doesn't matter what mesh/compound instance is used for the function; just using it as a source of the function.
Debug.Assert(totalCompoundChildCount > 0);
Unsafe.AsRef<TCompoundB>(pairsToTest[0].Container).FindLocalOverlaps<CompoundPairOverlaps, ChildOverlapsCollection>(ref pairsToTest, pool, shapes, ref overlaps);

Unsafe.AsRef<TCompoundB>(pairsToTest[0].Container).FindLocalOverlaps<CompoundPairOverlaps, ChildOverlapsCollection>(ref pairsToTest, pool, shapes, ref overlaps);

if (subpairData.Length > stackallocThreshold)
subpairData.Dispose(pool);
}

}
Expand Down

0 comments on commit fe8d87e

Please sign in to comment.