From d3e460605092b0b316651440b1e7a00c84284f05 Mon Sep 17 00:00:00 2001 From: Bohica <53943479+Bohicatv@users.noreply.github.com> Date: Thu, 12 Jun 2025 17:25:32 -0700 Subject: [PATCH] fix: Fixes SpanReader.Read to fix bounds checks and now returns bytes written (#2211) --- Projects/Server/Buffers/SpanReader.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Projects/Server/Buffers/SpanReader.cs b/Projects/Server/Buffers/SpanReader.cs index c127327c5..31c5fc51d 100644 --- a/Projects/Server/Buffers/SpanReader.cs +++ b/Projects/Server/Buffers/SpanReader.cs @@ -294,13 +294,17 @@ public ref struct SpanReader } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Read(Span bytes) + public int Read(Span 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; } }