From 160ba0938a23f6ec9462794deb478fd613ea80c5 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 13 Jun 2026 10:00:39 +0100 Subject: [PATCH] review fixes --- .../Compressors/Xz/XZBlock.Async.cs | 10 +++++--- src/SharpCompress/Compressors/Xz/XZBlock.cs | 23 ++++++++++++------- .../Streams/LzmaStreamAsyncTests.cs | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs b/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs index 0175faaf..cac658a0 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs @@ -1,3 +1,4 @@ +using System; using System.Buffers; using System.IO; using System.Linq; @@ -66,9 +67,12 @@ public sealed partial class XZBlock await BaseStream .ReadExactAsync(paddingBytes, 0, size, cancellationToken) .ConfigureAwait(false); - if (paddingBytes.Any(b => b != 0)) + for (var i = 0; i < size; i++) { - throw new InvalidFormatException("Padding bytes were non-null"); + if (paddingBytes[i] != 0) + { + throw new InvalidFormatException("Padding bytes were non-null"); + } } } finally @@ -87,7 +91,7 @@ public sealed partial class XZBlock await BaseStream .ReadExactAsync(crc, 0, _checkSize, cancellationToken) .ConfigureAwait(false); - VerifyCheck(crc); + VerifyCheck(crc.AsSpan().Slice(0, _checkSize)); _crcChecked = true; } finally diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.cs b/src/SharpCompress/Compressors/Xz/XZBlock.cs index 4221e3c4..bfb5f3b0 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.cs @@ -98,10 +98,17 @@ public sealed partial class XZBlock : XZReadOnlyStream private void CheckCrc() { - var crc = new byte[_checkSize]; - BaseStream.ReadExact(crc, 0, _checkSize); - VerifyCheck(crc); - _crcChecked = true; + var crc = ArrayPool.Shared.Rent(_checkSize); + try + { + BaseStream.ReadExact(crc, 0, _checkSize); + VerifyCheck(crc.AsSpan().Slice(0, _checkSize)); + _crcChecked = true; + } + finally + { + ArrayPool.Shared.Return(crc); + } } private void UpdateCheck(byte[] buffer, int offset, int count) @@ -127,7 +134,7 @@ public sealed partial class XZBlock : XZReadOnlyStream } } - private void VerifyCheck(byte[] expected) + private void VerifyCheck(ReadOnlySpan expected) { switch (_checkType) { @@ -147,7 +154,7 @@ public sealed partial class XZBlock : XZReadOnlyStream } } - private static void GetLittleEndianBytes(uint value, byte[] expected) + private static void GetLittleEndianBytes(uint value, ReadOnlySpan expected) { var bytes = ArrayPool.Shared.Rent(sizeof(uint)); try @@ -164,7 +171,7 @@ public sealed partial class XZBlock : XZReadOnlyStream } } - private static void GetLittleEndianBytes(ulong value, byte[] expected) + private static void GetLittleEndianBytes(ulong value, ReadOnlySpan expected) { var bytes = ArrayPool.Shared.Rent(sizeof(ulong)); try @@ -181,7 +188,7 @@ public sealed partial class XZBlock : XZReadOnlyStream } } - private void FinalizeSha256Check(byte[] expected) + private void FinalizeSha256Check(ReadOnlySpan expected) { _sha256.NotNull().TransformFinalBlock(Array.Empty(), 0, 0); if (!expected.SequenceEqual(_sha256.NotNull().Hash)) diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs index 5dd0348f..5a32b4ee 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs @@ -17,7 +17,7 @@ public class LzmaStreamAsyncTests : TestBase { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "bad-1-lzma2-7.xz")); - var xz = new XZStream(stream); + using var xz = new XZStream(stream); await Assert.ThrowsAsync(async () => await xz.TransferToAsync(Stream.Null, long.MaxValue) );