From b76184325d02c0ceb6b166d77bc04664cdc35c6b Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Thu, 10 Oct 2024 11:39:00 +1300 Subject: [PATCH] Truncate StreamDecorator reads Fixes: #184 --- sources/OpenMcdf.Extensions/StreamDecorator.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sources/OpenMcdf.Extensions/StreamDecorator.cs b/sources/OpenMcdf.Extensions/StreamDecorator.cs index ee3b04a1..b79202b8 100644 --- a/sources/OpenMcdf.Extensions/StreamDecorator.cs +++ b/sources/OpenMcdf.Extensions/StreamDecorator.cs @@ -66,6 +66,11 @@ public override int Read(byte[] buffer, int offset, int count) if (position >= cfStream.Size) return 0; + int maxReadableLength = (int)Math.Min(int.MaxValue, Length - Position); + count = Math.Max(0, Math.Min(maxReadableLength, count)); + if (count == 0) + return 0; + count = cfStream.Read(buffer, position, offset, count); position += count; return count;