From f6b6aa1e149fa525e77b14aefe26e16ed886d7ad Mon Sep 17 00:00:00 2001 From: gmipf Date: Thu, 15 Oct 2026 03:20:30 +0200 Subject: [PATCH] Allow 0-byte reads at the end of a byte array (#3) * Allow 0-byte reads at the end of a byte array ReadBytes(ref offset, 0) threw ArgumentOutOfRangeException when offset was equal to the array length, before the 0-byte case was handled. The same happened for a 0-byte read from an empty array, and TryReadBytes with a count of 0 at the end threw instead of returning true. The offset check now accepts the end of the array for a 0-byte read only; reads past the end still throw. Co-Authored-By: Claude Opus 5.5 * Handle 0-byte reads before the offset check Same order as the stream version of ReadExactlyToBuffer, applied to both byte array copies (Numerics.Extensions and IO.Extensions). Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../ByteArrayReaderExtensions.cs | 8 ++--- .../ByteArrayReaderExtensionsTests.cs | 34 +++++++++++++++++++ .../ByteArrayReaderExtensions.cs | 8 ++--- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs b/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs index c9a120a..764b9a0 100644 --- a/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs +++ b/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs @@ -240,10 +240,6 @@ namespace SabreTools.IO.Extensions /// private static byte[] ReadExactlyToBuffer(byte[] content, ref int offset, int length) { - // If we have an invalid offset - if (offset < 0 || offset >= content.Length) - throw new ArgumentOutOfRangeException($"{nameof(offset)} must be between 0 and {content.Length}, {offset} provided"); - // If we have an invalid length if (length < 0) throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested"); @@ -252,6 +248,10 @@ namespace SabreTools.IO.Extensions if (length == 0) return []; + // If we have an invalid offset + if (offset < 0 || offset >= content.Length) + throw new ArgumentOutOfRangeException($"{nameof(offset)} must be between 0 and {content.Length}, {offset} provided"); + // If there are not enough bytes if (offset + length > content.Length) throw new System.IO.EndOfStreamException($"Requested to read {length} bytes from {nameof(content)}, {content.Length - offset} returned"); diff --git a/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs index 961ee87..f5a47b9 100644 --- a/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs +++ b/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs @@ -70,6 +70,31 @@ namespace SabreTools.Numerics.Extensions.Test Assert.True(read.SequenceEqual(_bytes.Take(length))); } + [Fact] + public void ReadBytesZeroLengthAtEndTest() + { + int offset = _bytes.Length; + byte[] read = _bytes.ReadBytes(ref offset, 0); + Assert.Empty(read); + Assert.Equal(_bytes.Length, offset); + } + + [Fact] + public void ReadBytesZeroLengthEmptyArrayTest() + { + int offset = 0; + byte[] read = Array.Empty().ReadBytes(ref offset, 0); + Assert.Empty(read); + Assert.Equal(0, offset); + } + + [Fact] + public void ReadBytesPastEndTest() + { + int offset = _bytes.Length; + Assert.Throws(() => _bytes.ReadBytes(ref offset, 1)); + } + [Fact] public void ReadSByteTest() { @@ -1443,6 +1468,15 @@ namespace SabreTools.Numerics.Extensions.Test Assert.Empty(read); } + [Fact] + public void TryReadBytesZeroLengthAtEndTest() + { + int offset = _bytes.Length; + bool actual = _bytes.TryReadBytes(ref offset, 0, out byte[] read); + Assert.True(actual); + Assert.Empty(read); + } + [Fact] public void TryReadSByteTest() { diff --git a/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs b/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs index b29ba8b..662ed30 100644 --- a/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs +++ b/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs @@ -782,10 +782,6 @@ namespace SabreTools.Numerics.Extensions /// private static byte[] ReadExactlyToBuffer(byte[] content, ref int offset, int length) { - // If we have an invalid offset - if (offset < 0 || offset >= content.Length) - throw new ArgumentOutOfRangeException($"{nameof(offset)} must be between 0 and {content.Length}, {offset} provided"); - // If we have an invalid length if (length < 0) throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested"); @@ -794,6 +790,10 @@ namespace SabreTools.Numerics.Extensions if (length == 0) return []; + // If we have an invalid offset + if (offset < 0 || offset >= content.Length) + throw new ArgumentOutOfRangeException($"{nameof(offset)} must be between 0 and {content.Length}, {offset} provided"); + // If there are not enough bytes if (offset + length > content.Length) throw new System.IO.EndOfStreamException($"Requested to read {length} bytes from {nameof(content)}, {content.Length - offset} returned");