From 72dec0b203f47952a3f9bd4b68fce22dc4956342 Mon Sep 17 00:00:00 2001 From: sakno Date: Wed, 4 Dec 2024 20:55:26 +0200 Subject: [PATCH] Fixed corner case when the internal buffer is empty --- src/DotNext/Buffers/BufferWriterSlim.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/DotNext/Buffers/BufferWriterSlim.cs b/src/DotNext/Buffers/BufferWriterSlim.cs index 7ef68984c..8443bdddc 100644 --- a/src/DotNext/Buffers/BufferWriterSlim.cs +++ b/src/DotNext/Buffers/BufferWriterSlim.cs @@ -309,7 +309,13 @@ public bool TryDetachBuffer(out MemoryOwner owner) public MemoryOwner DetachOrCopyBuffer() { MemoryOwner result; - if (NoOverflow) + + if (position is 0) + { + result = default; + goto exit; + } + else if (NoOverflow) { result = allocator.AllocateExactly(position); initialBuffer.CopyTo(result.Span); @@ -319,9 +325,11 @@ public MemoryOwner DetachOrCopyBuffer() result = extraBuffer; extraBuffer = default; } - + result.Truncate(position); position = 0; + + exit: return result; }