Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unsafe modifier from Memory<T>.Span #106085

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public Memory<T> Slice(int start, int length)
/// <summary>
/// Returns a span from the memory.
/// </summary>
public unsafe Span<T> Span
public Span<T> Span
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
Expand Down Expand Up @@ -327,12 +327,14 @@ public unsafe Span<T> Span
// least to be in-bounds when compared with the original Memory<T> instance, so using the span won't
// AV the process.

// We use 'nuint' because it gives us a free early zero-extension to 64 bits when running on a 64-bit platform.
nuint desiredStartIndex = (uint)_index & (uint)ReadOnlyMemory<T>.RemoveFlagsBitMask;

int desiredLength = _length;

#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan)
if ((ulong)(uint)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan)
xtqqczze marked this conversation as resolved.
Show resolved Hide resolved
{
ThrowHelper.ThrowArgumentOutOfRangeException();
}
Expand All @@ -343,7 +345,7 @@ public unsafe Span<T> Span
}
#endif

refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex);
refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex);
lengthOfUnderlyingSpan = desiredLength;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public ReadOnlyMemory<T> Slice(int start, int length)
/// <summary>
/// Returns a span from the memory.
/// </summary>
public unsafe ReadOnlySpan<T> Span
public ReadOnlySpan<T> Span
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
Expand Down Expand Up @@ -249,7 +249,9 @@ public unsafe ReadOnlySpan<T> Span
// least to be in-bounds when compared with the original Memory<T> instance, so using the span won't
// AV the process.

// We use 'nuint' because it gives us a free early zero-extension to 64 bits when running on a 64-bit platform.
nuint desiredStartIndex = (uint)_index & (uint)RemoveFlagsBitMask;

int desiredLength = _length;

#if TARGET_64BIT
Expand All @@ -265,7 +267,7 @@ public unsafe ReadOnlySpan<T> Span
}
#endif

refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex);
refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex);
lengthOfUnderlyingSpan = desiredLength;
}

Expand Down
Loading