diff --git a/src/libraries/System.IO.Compression/src/System/IO/Compression/Zstandard/ZstandardDictionary.cs b/src/libraries/System.IO.Compression/src/System/IO/Compression/Zstandard/ZstandardDictionary.cs index 5672690ae458f0..046e97a90bed3d 100644 --- a/src/libraries/System.IO.Compression/src/System/IO/Compression/Zstandard/ZstandardDictionary.cs +++ b/src/libraries/System.IO.Compression/src/System/IO/Compression/Zstandard/ZstandardDictionary.cs @@ -103,6 +103,7 @@ public static ZstandardDictionary Train(ReadOnlySpan samples, ReadOnlySpan // This incidentally also protects against concurrent modifications of the sampleLengths that could cause // access violations later in native code. byte[] lengthsArray = ArrayPool.Shared.Rent(sampleLengths.Length * Unsafe.SizeOf()); + byte[]? dictionaryBuffer = null; try { Span lengthsAsNuint = MemoryMarshal.Cast(lengthsArray.AsSpan(0, sampleLengths.Length * Unsafe.SizeOf())); @@ -127,8 +128,7 @@ public static ZstandardDictionary Train(ReadOnlySpan samples, ReadOnlySpan ArgumentOutOfRangeException.ThrowIfLessThan(maxDictionarySize, 256, nameof(maxDictionarySize)); - byte[] dictionaryBuffer = new byte[maxDictionarySize]; - + dictionaryBuffer = ArrayPool.Shared.Rent(maxDictionarySize); nuint dictSize; unsafe @@ -148,6 +148,10 @@ public static ZstandardDictionary Train(ReadOnlySpan samples, ReadOnlySpan } finally { + if (dictionaryBuffer is not null) + { + ArrayPool.Shared.Return(dictionaryBuffer); + } ArrayPool.Shared.Return(lengthsArray); } }