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; }