Skip to content

Inconsistent Exceptions for ReadOnlyMemory.Slice #90939

@Darcara

Description

@Darcara

Description

ReadOnlyMemory.Slice(start, length) will throw an ArgumentOutOfRangeException with the Parameter 'start', even if the start parameter is actually in the range of the ReadOnlyMemory and length is out of bounds.

Memory.Slice(start, length) will throw the same exception without paramName set.

Span.Slice and ReadOnlySpan.Slice will always throw ArgumentOutOfRangeException without paramName set

Reproduction Steps

// ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
Memory<Byte>.Empty.Slice(4);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Memory<Byte>.Empty.Slice(4, 5);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
ReadOnlyMemory<Byte>.Empty.Slice(4);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
ReadOnlyMemory<Byte>.Empty.Slice(4, 5);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
new ReadOnlyMemory<Byte>(new Byte[5]).Slice(4, 5);

// ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Span<Byte>.Empty.Slice(4);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Span<Byte>.Empty.Slice(4, 5);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
ReadOnlySpan<Byte>.Empty.Slice(4);
// ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
ReadOnlySpan<Byte>.Empty.Slice(4, 5);

Expected behavior

Span, ReadOnlySpan, Memory and ReadOnlyMemory should have the same behavior. I would expect at least Memory and ReadOnlyMemory to be consistent.

Case 5: new ReadOnlyMemory<Byte>(new Byte[5]).Slice(4, 5) should throw an exception without paramName set (to be consistent with the others), or preferably complain about length, if the others start doing so too.

Actual behavior

see reproduction steps

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
 Version:   7.0.400
 Commit:    73bf45718d
Host:
  Version:      7.0.10
  Architecture: x64
  Commit:       a6dbb800a4 

Persists at least since SDK: 7.0.202 - Host: 7.0.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Memoryneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions