Skip to content

Commit

Permalink
Fixed corner case when the internal buffer is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Dec 4, 2024
1 parent b96205d commit 72dec0b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/DotNext/Buffers/BufferWriterSlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,13 @@ public bool TryDetachBuffer(out MemoryOwner<T> owner)
public MemoryOwner<T> DetachOrCopyBuffer()
{
MemoryOwner<T> result;
if (NoOverflow)

if (position is 0)
{
result = default;
goto exit;
}
else if (NoOverflow)
{
result = allocator.AllocateExactly(position);
initialBuffer.CopyTo(result.Span);
Expand All @@ -319,9 +325,11 @@ public MemoryOwner<T> DetachOrCopyBuffer()
result = extraBuffer;
extraBuffer = default;
}

result.Truncate(position);
position = 0;

exit:
return result;
}

Expand Down

0 comments on commit 72dec0b

Please sign in to comment.