Skip to content

Validate ZstandardStream constructor arguments and prevent ArrayPool buffer leak on failure - #135934

Merged
rzikm merged 1 commit into
dotnet:mainfrom
austinaviv:fix/zstandardstream-ctor-arg-validation
Oct 16, 2026
Merged

rzikm merged 1 commit into
dotnet:mainfrom
austinaviv:fix/zstandardstream-ctor-arg-validation

Conversation

@austinaviv

Copy link
Copy Markdown
Contributor

Fixes #135933

Description

This PR addresses constructor argument validation and resource cleanup in ZstandardStream:

  • Added ArgumentNullException.ThrowIfNull(compressionOptions) before calling Init in ZstandardStream(Stream, ZstandardCompressionOptions, bool), and updated the XML documentation.
  • Resolved quality before Init in ZstandardStream(Stream, CompressionLevel, bool) so invalid CompressionLevel enum values throw ArgumentOutOfRangeException before renting the internal 64 KB buffer.
  • Wrapped encoder and decoder allocations following Init in try-catch blocks to dispose _buffer if native context allocation or configuration throws.
  • Added unit tests covering null compression options and out-of-range compression levels.

…buffer leak on failure

- Validate compressionOptions with ArgumentNullException.ThrowIfNull before calling Init in ZstandardStream(Stream, ZstandardCompressionOptions, bool).

- Validate compressionLevel before calling Init in ZstandardStream(Stream, CompressionLevel, bool) to avoid renting a buffer when GetQualityFromCompressionLevel throws ArgumentOutOfRangeException.

- Dispose _buffer in try-catch blocks if encoder or decoder instantiation fails after Init.

- Add unit tests for constructor argument validation.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

@austinaviv

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@rzikm rzikm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Compression community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZstandardStream: Validate compressionOptions and prevent ArrayPool buffer leak on constructor failure

2 participants