From 0c35abdebe5c398e106956e024e8264d8e3055ae Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 20 Feb 2021 12:52:29 +0000 Subject: [PATCH] Explicit exception for read shortcut --- src/SharpCompress/Utility.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index 8f54b837..0f879a3b 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -21,8 +21,13 @@ namespace SharpCompress public static async ValueTask ReadPrimitive(this Stream stream, int bytes, Func, T> func, CancellationToken cancellationToken) { using var buffer = MemoryPool.Shared.Rent(bytes); - await stream.ReadAsync(buffer.Memory.Slice(0, bytes), cancellationToken); - return func(buffer.Memory.Slice(0, bytes)); + var memory = buffer.Memory.Slice(0, bytes); + var n = await stream.ReadAsync(memory, cancellationToken); + if (n != memory.Length) + { + throw new InvalidOperationException("Unexpected length"); + } + return func(memory); } public static ValueTask ReadByteAsync(this Stream stream, CancellationToken cancellationToken)