You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ZstandardStream: Validate compressionOptions and prevent ArrayPool buffer leak on constructor failure #135933
Additionally, the XML doc comment on ZstandardStream(Stream, ZstandardCompressionOptions, bool) only lists stream under <exception cref="ArgumentNullException"> and misses compressionOptions.
Beyond the missing validation, Init allocates a 64 KB buffer rented from ArrayPool<byte>.Shared. If an exception is thrown after Init completes:
When calling new ZstandardStream(stream, (CompressionLevel)invalidValue), Init is called before ZstandardUtils.GetQualityFromCompressionLevel(compressionLevel) throws ArgumentOutOfRangeException.
If encoder or decoder initialization fails inside any ZstandardStream constructor after Init has rented the buffer.
In these cases, the stream is not constructed, Dispose is never called by the consumer, and the rented 64 KB array pool buffer is leaked permanently.
Reproduction
usingSystem.IO.Compression;usingvarms=newMemoryStream();// Missing null check: throws inside ZstandardEncoder after Init has already rented the buffertry{_=newZstandardStream(ms,(ZstandardCompressionOptions)null!);}catch(ArgumentNullException){}// Leaks 64 KB from ArrayPool per invalid callfor(inti=0;i<500;i++){try{_=newZstandardStream(ms,(CompressionLevel)999);}catch(ArgumentOutOfRangeException){}}
Expected Behavior
ZstandardStream(Stream, ZstandardCompressionOptions, bool) should validate compressionOptions with ArgumentNullException.ThrowIfNull(compressionOptions) before calling Init.
ZstandardStream(Stream, CompressionLevel, bool) should validate compressionLevel before calling Init.
If encoder or decoder instantiation throws after Init, _buffer.Dispose() should be called to return the rented buffer to the pool.
Activity
dotnet-policy-service commentedon Oct 16, 2026
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.
Validate ZstandardStream constructor arguments and prevent ArrayPool …