-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Open
Labels
Milestone
Description
In
runtime/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Lines 135 to 154 in dbb05eb
| public static ReadOnlySpan<char> AsSpan(this string? text, int start, int length) | |
| { | |
| if (text == null) | |
| { | |
| if (start != 0 || length != 0) | |
| ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); | |
| return default; | |
| } | |
| #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); | |
| #else | |
| if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) | |
| ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); | |
| #endif | |
| return new ReadOnlySpan<char>(ref Unsafe.Add(ref text.GetRawStringData(), (nint)(uint)start /* force zero-extension */), length); | |
| } |
ExceptionArgument.start is used, but length may be the faulty argument.
runtime/src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs
Lines 350 to 362 in dbb05eb
| 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(); | |
| #else | |
| if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start)) | |
| ThrowHelper.ThrowArgumentOutOfRangeException(); | |
| #endif | |
| return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start /* force zero-extension */), length); | |
| } |
only throws
ArgumentOutOfRangeException without specifying the paramname.{RO}S ctor does it the same way.
Similar for AsMemory(this string? text, int start, int length);.
Should the behavior of MemoryExtensions.AsSpan/AsMemory be aligned to the one of Slice?
For reference see also #53463 (comment)