fix: Fixes SpanReader.Read to fix bounds checks and now returns bytes written (#2211)

This commit is contained in:
Bohica 2025-06-12 17:25:32 -07:00 • committed by GitHub
parent a2ea4bda37
commit d3e4606050
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -294,13 +294,17 @@ public ref struct SpanReader
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Read(Span<byte> bytes)
public int Read(Span<byte> bytes)
{
if (bytes.Length < Length)
if (bytes.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(bytes));
return 0;
}
return _buffer.TryCopyTo(bytes);
var bytesWritten = Math.Min(bytes.Length, Remaining);
_buffer.Slice(Position, bytesWritten).CopyTo(bytes);
Position += bytesWritten;
return bytesWritten;
}
}