CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.99k stars 294 forks source link

Error in IBufferWriterExtension Write for unmanaged types #798

Closed pziezio closed 1 month ago

pziezio commented 10 months ago

Describe the bug

IBufferWriterExtension.Write method for unmanaged types creates a Span with invalid length:

int length = sizeof(T);
Span<byte> span = writer.GetSpan(1);

should be:

int length = sizeof(T);
Span<byte> span = writer.GetSpan(length);

That causes System.ArgumentException : The current buffer writer can't contain the requested input data. at runtime.

Steps to reproduce

Leave only one byte of free capacity and write a larger unmanaged type:

using var buffer = new ArrayPoolBufferWriter<byte>(16);

var count = buffer.Capacity - 1;
for (int i = 0; i < count; i++)
{
    buffer.Write<byte>(0);
}

buffer.Write(1.0);

Expected behavior

It should grow the buffer accordingly.

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.2.2

Help us help you

Yes, I'd like to be assigned to work on this item

pziezio commented 1 month ago

Would it be possible to merge this? At the moment IBufferWriterExtension is unusable because of this bug.