From 74294567e7bfacfd5dec4c9e5228d7749eab2b20 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Tue, 29 Oct 2024 12:34:26 -0400 Subject: [PATCH] Harden softer against invalid comparers --- BeefLibs/corlib/src/Collections/Sorter.bf | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/BeefLibs/corlib/src/Collections/Sorter.bf b/BeefLibs/corlib/src/Collections/Sorter.bf index 4e267e36..71a735d9 100644 --- a/BeefLibs/corlib/src/Collections/Sorter.bf +++ b/BeefLibs/corlib/src/Collections/Sorter.bf @@ -219,9 +219,24 @@ namespace System.Collections while (left < right) { - while (comparer(keys[++left], pivot) < 0) {} - while (comparer(pivot, keys[--right]) < 0) {} + while (true) + { + left++; + if (left >= mCount) + Runtime.FatalError("Illegal comparer method. Comparer(lhs, rhs) must equal -Comparer(rhs, lhs)."); + if (comparer(keys[++left], pivot) >= 0) + break; + } + while (true) + { + right--; + if (right < 0) + Runtime.FatalError("Illegal comparer method. Comparer(lhs, rhs) must equal -Comparer(rhs, lhs)."); + if (comparer(pivot, keys[right]) >= 0) + break; + } + if(left >= right) break;