From 5e7644bd1857840ef876bbf3317aa7c0a91dfe59 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 5 Apr 2026 14:11:48 +0100 Subject: [PATCH] Update src/SharpCompress/IO/PooledMemoryStream.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/SharpCompress/IO/PooledMemoryStream.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/SharpCompress/IO/PooledMemoryStream.cs b/src/SharpCompress/IO/PooledMemoryStream.cs index 9cd19696..a832bf69 100644 --- a/src/SharpCompress/IO/PooledMemoryStream.cs +++ b/src/SharpCompress/IO/PooledMemoryStream.cs @@ -892,11 +892,10 @@ public sealed class PooledMemoryStream : MemoryStream _contiguousBuffer = null; } - for (var i = 0; i < _detachedExposedBuffers.Count; i++) - { - _arrayPool.Return(_detachedExposedBuffers[i]); - } - + // Buffers tracked here have been exposed to callers. Returning them to the + // shared pool would allow unrelated code to rent and mutate arrays that may + // still be referenced after the stream is disposed, which breaks + // MemoryStream-compatible expectations for GetBuffer/TryGetBuffer. _detachedExposedBuffers.Clear(); }