Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,16 @@ public Memory<T> Slice(int start, int length)
{
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,16 @@ public static ReadOnlySpan<char> AsSpan(this string? text, int start, int length

#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}
#else
if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
Expand Down Expand Up @@ -227,8 +235,16 @@ public static ReadOnlyMemory<char> AsMemory(this string? text, int start, int le

#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}
#else
if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
Expand Down
24 changes: 20 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ public ReadOnlyMemory(T[]? array, int start, int length)
}
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down Expand Up @@ -178,8 +186,16 @@ public ReadOnlyMemory<T> Slice(int start, int length)
{
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
Expand Down
24 changes: 20 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ public ReadOnlySpan(T[]? array, int start, int length)
}
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down Expand Up @@ -361,8 +369,16 @@ public ReadOnlySpan<T> Slice(int start, int length)
{
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down
25 changes: 21 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ public Span(T[]? array, int start, int length)
ThrowHelper.ThrowArrayTypeMismatchException();
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down Expand Up @@ -413,8 +421,17 @@ public Span<T> Slice(int start, int length)
// of this is that if either input is negative or if the input sum overflows past Int32.MaxValue,
// that information is captured correctly in the comparison against the backing _length field.
// We don't use this same mechanism in a 32-bit process due to the overhead of 64-bit arithmetic.
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
// We special-case "start == 0" to optimize codegen since the pattern Slice(0, ...) is so common.
if (RuntimeHelpers.IsKnownConstant(start) && start == 0)
{
if ((uint)length > (uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
else
{
if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
ThrowHelper.ThrowArgumentOutOfRangeException();
}
#else
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down