From 6d975f9a52fadd67b8f64fc3771a4920bc310e5e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 7 Jan 2022 14:03:33 -0800 Subject: [PATCH] [release/5.0] Fixes #62167. WriteAsync may truncate data if called after .Advance(int) (#62350) * Fixes #62167. WriteAsync was bugged when writing multiple segments if called after an .Advance(int) * Add packaging changes Co-authored-by: Kuinox Co-authored-by: Santiago Fernandez Madero --- .../System.IO.Pipelines/Directory.Build.props | 2 +- .../src/System/IO/Pipelines/Pipe.cs | 2 +- .../System.IO.Pipelines/tests/PipeLengthTests.cs | 14 ++++++++++++++ src/libraries/libraries-packages.proj | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.IO.Pipelines/Directory.Build.props b/src/libraries/System.IO.Pipelines/Directory.Build.props index 0daa67b02c97b..81ab65eae172c 100644 --- a/src/libraries/System.IO.Pipelines/Directory.Build.props +++ b/src/libraries/System.IO.Pipelines/Directory.Build.props @@ -2,7 +2,7 @@ Open - 1 + 2 5.0.0.1 diff --git a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs index f90ac5b8a9b9f..0149c04051d91 100644 --- a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs +++ b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs @@ -1011,7 +1011,7 @@ private void WriteMultiSegment(ReadOnlySpan source) } // We filled the segment - _writingHead.End += writable; + _writingHead.End += _writingHeadBytesBuffered; _writingHeadBytesBuffered = 0; // This is optimized to use pooled memory. That's why we pass 0 instead of diff --git a/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs b/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs index fcf9d7af55b05..fd9fc83833a8b 100644 --- a/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs +++ b/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs @@ -277,5 +277,19 @@ public async Task NullExaminedAndConsumedNoops() ReadResult result = await _pipe.Reader.ReadAsync(); _pipe.Reader.AdvanceTo(default, default); } + + [Fact] + public async Task AdvanceFollowedByWriteAsyncTest() + { + Memory buffer = new byte[26]; + Pipe pipe = new(new PipeOptions(minimumSegmentSize: 1)); + + var mem = pipe.Writer.GetMemory(14)[..14]; + buffer[..14].CopyTo(mem); + pipe.Writer.Advance(14); + await pipe.Writer.WriteAsync(buffer[14..]); + ReadResult res = await pipe.Reader.ReadAsync(); + Assert.Equal(res.Buffer.Length, buffer.Length); + } } } diff --git a/src/libraries/libraries-packages.proj b/src/libraries/libraries-packages.proj index 87660e3c2483c..00d67ec988127 100644 --- a/src/libraries/libraries-packages.proj +++ b/src/libraries/libraries-packages.proj @@ -18,6 +18,7 @@ +