fix: Fixes NPE in PooledRefList sort (#1864)
This commit is contained in:
parent
20855ddff3
commit
96de1fdaa1
1 changed files with 13 additions and 19 deletions
|
|
@ -6,6 +6,7 @@ using System.Buffers;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using Server.Buffers;
|
using Server.Buffers;
|
||||||
|
|
||||||
namespace Server.Collections;
|
namespace Server.Collections;
|
||||||
|
|
@ -946,13 +947,16 @@ public ref struct PooledRefList<T>
|
||||||
_version++;
|
_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
|
// Sorts the elements in this list. Uses Array.Sort with the
|
||||||
// provided comparer.
|
// 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
|
// Sorts the elements in a section of this list. The sort compares the
|
||||||
// elements to each other using the given IComparer interface. If
|
// elements to each other using the given IComparer interface. If
|
||||||
|
|
@ -964,15 +968,8 @@ public ref struct PooledRefList<T>
|
||||||
//
|
//
|
||||||
public void Sort(int index, int count, IComparer<T>? comparer)
|
public void Sort(int index, int count, IComparer<T>? comparer)
|
||||||
{
|
{
|
||||||
if (index < 0)
|
ArgumentOutOfRangeException.ThrowIfNegative(index);
|
||||||
{
|
ArgumentOutOfRangeException.ThrowIfNegative(count);
|
||||||
throw new ArgumentOutOfRangeException(nameof(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count < 0)
|
|
||||||
{
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(count));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_size - index < count)
|
if (_size - index < count)
|
||||||
{
|
{
|
||||||
|
|
@ -988,14 +985,11 @@ public ref struct PooledRefList<T>
|
||||||
|
|
||||||
public void Sort(Comparison<T> comparison)
|
public void Sort(Comparison<T> comparison)
|
||||||
{
|
{
|
||||||
if (comparison == null)
|
ArgumentNullException.ThrowIfNull(comparison);
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(comparison));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_size > 1)
|
if (_size > 1)
|
||||||
{
|
{
|
||||||
Array.Sort(_items, comparison);
|
_items.AsSpan(0, _size).Sort(comparison);
|
||||||
}
|
}
|
||||||
_version++;
|
_version++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue