Skip to content

Commit 72dec0b

Browse files
committed
Fixed corner case when the internal buffer is empty
1 parent b96205d commit 72dec0b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/DotNext/Buffers/BufferWriterSlim.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,13 @@ public bool TryDetachBuffer(out MemoryOwner<T> owner)
309309
public MemoryOwner<T> DetachOrCopyBuffer()
310310
{
311311
MemoryOwner<T> result;
312-
if (NoOverflow)
312+
313+
if (position is 0)
314+
{
315+
result = default;
316+
goto exit;
317+
}
318+
else if (NoOverflow)
313319
{
314320
result = allocator.AllocateExactly(position);
315321
initialBuffer.CopyTo(result.Span);
@@ -319,9 +325,11 @@ public MemoryOwner<T> DetachOrCopyBuffer()
319325
result = extraBuffer;
320326
extraBuffer = default;
321327
}
322-
328+
323329
result.Truncate(position);
324330
position = 0;
331+
332+
exit:
325333
return result;
326334
}
327335

0 commit comments

Comments
 (0)