Skip to content

Reachable Debug.Assert in StreamPipeReader #123809

@vcsjones

Description

@vcsjones

The following unit test intentionally misuses PipeReader, however it does hit a Debug.Assert.

Debug.Assert is generally meant to hold invariants to be true, meaning misuse or not, they should not be reachable from public APIs.

[Fact]
public async Task PipeReaderPositionMisuseDebugAssert()
{
    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;
    reader1.AdvanceTo(posFrom2);
}

Will result in the following test failure:

Stack Trace:
     at Microsoft.VisualStudio.TestPlatform.TestHost.TestHostTraceListener.GetException(String message)
   at Microsoft.VisualStudio.TestPlatform.TestHost.TestHostTraceListener.Fail(String message, String detailMessage)
   at System.Diagnostics.TraceInternal.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceInternal.cs:line 261
   at System.Diagnostics.TraceInternal.TraceProvider.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Diagnostics.TraceSource/src/System/Diagnostics/TraceInternal.cs:line 17
   at System.Diagnostics.Debug.Fail(String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 135
   at System.Diagnostics.Debug.Assert(Boolean condition, String message, String detailMessage) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 99
   at System.Diagnostics.Debug.Assert(Boolean condition, String message) in /Users/vcsjones/Projects/runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs:line 88
   at System.IO.Pipelines.StreamPipeReader.AdvanceTo(BufferSegment consumedSegment, Int32 consumedIndex, BufferSegment examinedSegment, Int32 examinedIndex) in /Users/vcsjones/Projects/runtime/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/StreamPipeReader.cs:line 111
   at System.IO.Pipelines.StreamPipeReader.AdvanceTo(SequencePosition consumed, SequencePosition examined) in /Users/vcsjones/Projects/runtime/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/StreamPipeReader.cs:line 89
   at System.IO.Pipelines.StreamPipeReader.AdvanceTo(SequencePosition consumed) in /Users/vcsjones/Projects/runtime/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/StreamPipeReader.cs:line 70

Metadata

Metadata

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions