Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ private void AdvanceTo(BufferSegment? consumedSegment, int consumedIndex, Buffer

long consumedBytes = BufferSegment.GetLength(returnStart, _readIndex, consumedSegment, consumedIndex);

_bufferedBytes -= consumedBytes;
if (consumedBytes < 0 || consumedBytes > _bufferedBytes)
{
ThrowHelper.ThrowInvalidOperationException_AdvanceToInvalidCursor();
}

Debug.Assert(_bufferedBytes >= 0);
_bufferedBytes -= consumedBytes;

_examinedEverything = false;

Expand Down
16 changes: 16 additions & 0 deletions src/libraries/System.IO.Pipelines/tests/StreamPipeReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,22 @@ public async Task InvalidCursorThrows()
reader.Complete();
}

[Fact]
public async Task AdvanceWithPositionFromDifferentReaderThrows()
{
PipeReader reader1 = PipeReader.Create(new MemoryStream(new byte[10]));
PipeReader reader2 = PipeReader.Create(new MemoryStream(new byte[1000]));

ReadResult result1 = await reader1.ReadAsync();
ReadResult result2 = await reader2.ReadAsync();

SequencePosition posFrom2 = result2.Buffer.End;
Assert.Throws<InvalidOperationException>(() => reader1.AdvanceTo(posFrom2));

reader1.Complete();
reader2.Complete();
}

[Fact]
public void NullStreamThrows()
{
Expand Down
Loading