Skip to content

Commit

Permalink
fix: Fixes NPE in PooledRefList sort (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman authored Jul 7, 2024
1 parent 20855dd commit 96de1fd
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions Projects/Server/Collections/PooledRefList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Server.Buffers;

namespace Server.Collections;
Expand Down Expand Up @@ -946,13 +947,16 @@ public void Reverse(int index, int count)
_version++;
}

// Sorts the elements in this list. Uses the default comparer and
// Array.Sort.
public void Sort() => Sort(0, Count, null);

// Sorts the elements in this list. Uses Array.Sort with the
// provided comparer.
public void Sort(IComparer<T>? comparer) => Sort(0, Count, comparer);
public void Sort(IComparer<T>? comparer = null)
{
if (_size > 1)
{
Array.Sort(_items, 0, _size, comparer);
}
_version++;
}

// Sorts the elements in a section of this list. The sort compares the
// elements to each other using the given IComparer interface. If
Expand All @@ -964,15 +968,8 @@ public void Reverse(int index, int count)
//
public void Sort(int index, int count, IComparer<T>? comparer)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}

if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfNegative(count);

if (_size - index < count)
{
Expand All @@ -988,14 +985,11 @@ public void Sort(int index, int count, IComparer<T>? comparer)

public void Sort(Comparison<T> comparison)
{
if (comparison == null)
{
throw new ArgumentNullException(nameof(comparison));
}
ArgumentNullException.ThrowIfNull(comparison);

if (_size > 1)
{
Array.Sort(_items, comparison);
_items.AsSpan(0, _size).Sort(comparison);
}
_version++;
}
Expand Down

0 comments on commit 96de1fd

Please sign in to comment.