Skip to content

Commit 3a4307e

Browse files
authored
Fix Array.Sort in the presence of nulls (#41234)
* Add failing test case * Fix Array.Sort in the presence of nulls
1 parent 4b431b5 commit 3a4307e

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private static int PickPivotAndPartition(Span<T> keys)
474474
if (pivot == null)
475475
{
476476
while (Unsafe.IsAddressLessThan(ref leftRef, ref nextToLastRef) && (leftRef = ref Unsafe.Add(ref leftRef, 1)) == null) ;
477-
while (Unsafe.IsAddressGreaterThan(ref rightRef, ref zeroRef) && (rightRef = ref Unsafe.Add(ref rightRef, -1)) == null) ;
477+
while (Unsafe.IsAddressGreaterThan(ref rightRef, ref zeroRef) && (rightRef = ref Unsafe.Add(ref rightRef, -1)) != null) ;
478478
}
479479
else
480480
{

src/libraries/System.Runtime/tests/System/ArrayTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,7 @@ public static IEnumerable<object[]> Sort_SZArray_TestData()
33263326
yield return new object[] { new string[] { "5", "2", "9", "8", "4", "3", "2", "4", "6" }, 0, 9, new StringComparer(), new string[] { "2", "2", "3", "4", "4", "5", "6", "8", "9" } };
33273327
yield return new object[] { new string[] { "5", null, "2", "9", "8", "4", "3", "2", "4", "6" }, 0, 10, new StringComparer(), new string[] { null, "2", "2", "3", "4", "4", "5", "6", "8", "9" } };
33283328
yield return new object[] { new string[] { "5", null, "2", "9", "8", "4", "3", "2", "4", "6" }, 3, 4, new StringComparer(), new string[] { "5", null, "2", "3", "4", "8", "9", "2", "4", "6" } };
3329+
yield return new object[] { new string[] { null, null, null, null, null, "foo", null, null, null, null, null, "bar", null, null, null, null, null }, 0, 17, null, new string[] { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "bar", "foo" } };
33293330
yield return new object[] { new int[] { 1, 2, 3, 4 }, 0, 4, null, new int[] { 1, 2, 3, 4 } };
33303331
yield return new object[] { new int[] { 4, 3, 2, 1 }, 0, 4, null, new int[] { 1, 2, 3, 4 } };
33313332
yield return new object[] { new int[] { 4, 3, 2, 1 }, 1, 2, null, new int[] { 4, 2, 3, 1 } };

0 commit comments

Comments
 (0)