Skip to content

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

Closed

Description

@austinaviv

Description

In ZstandardStream, the constructor accepting ZstandardCompressionOptions:

public ZstandardStream(Stream stream, ZstandardCompressionOptions compressionOptions, bool leaveOpen = false)

does not check compressionOptions for null before calling Init(stream, CompressionMode.Compress). In contrast, the decompression overload:

public ZstandardStream(Stream stream, ZstandardDecompressionOptions decompressionOptions, bool leaveOpen = false)

calls ArgumentNullException.ThrowIfNull(decompressionOptions).

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:

  1. When calling new ZstandardStream(stream, (CompressionLevel)invalidValue), Init is called before ZstandardUtils.GetQualityFromCompressionLevel(compressionLevel) throws ArgumentOutOfRangeException.
  2. 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

using System.IO.Compression;

using var ms = new MemoryStream();

// Missing null check: throws inside ZstandardEncoder after Init has already rented the buffer
try
{
    _ = new ZstandardStream(ms, (ZstandardCompressionOptions)null!);
}
catch (ArgumentNullException) { }

// Leaks 64 KB from ArrayPool per invalid call
for (int i = 0; i < 500; i++)
{
    try
    {
        _ = new ZstandardStream(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

  1. added
    untriaged
    on Oct 16, 2026
  2. dotnet-policy-service commented on Oct 16, 2026

    @dotnet-policy-service
    Contributor

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

  3. added a commit that references this issue on Oct 16, 2026
    de27411
  4. removed
    untriaged
    on Oct 16, 2026
  5. locked and limited conversation to collaborators on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions