runtime: Passing default SequencePosition to ReadOnlySequence.Slice shouldn't throw NullRef
string jsonString = "a";
ReadOnlyMemory<byte> dataMemory = Text.Encoding.UTF8.GetBytes(jsonString);
var firstSegment = new BufferSegment<byte>(dataMemory.Slice(0, 1));
ReadOnlyMemory<byte> secondMem = dataMemory.Slice(0, 0);
BufferSegment<byte> secondSegment = firstSegment.Append(secondMem);
// A two segment sequence where the second one is empty: [a]-> []
var sequence = new ReadOnlySequence<byte>(firstSegment, 0, secondSegment, secondMem.Length);
sequence = sequence.Slice(default(SequencePosition));
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
E:\GitHub\Fork\corefx\src\System.Memory\src\System\Buffers\ReadOnlySequence.Helpers.cs(243,0): at System.Buffers.ReadOnlySequence`1.BoundsCheck(SequencePosition& position)
E:\GitHub\Fork\corefx\src\System.Memory\src\System\Buffers\ReadOnlySequence.cs(416,0): at System.Buffers.ReadOnlySequence`1.Slice(SequencePosition start)
cc @davidfowl, @pakrym
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (16 by maintainers)
I think default(SequencePosition) should be “position 0”, and so: Slice(default, default) returns empty sequence Slice(default, x) returns sequence from beginning to x Slice(x, default) return empty if x == 0 and throws “end has to be after start” ix x > 0
I think Slice on
ros.Slice(0, default)
should behave the same way asspan.Slice(0, default)
ormemory.Slice(0, default)
ors.Substring(0, default)
.Assigning contextual meaning to
default(SP)
doesn’t feel right.@ahsonkhan I’m leaning towards treating default(Position) as “0” position.
So
ros.Slice(default)
would return the whole thing andros.Slice(0, default)
would return empty.