Skip to content

ZstandardDictionary.Train crashes the process (AccessViolation) for small sample sets instead of throwing #135264

Description

@neuecc

Description

System.IO.Compression.ZstandardDictionary.Train(ReadOnlySpan<byte>, ReadOnlySpan<int>, int) terminates the process with an access violation for a small, valid looking set of samples, instead of throwing an exception.

The cause is in the bundled zstd dictionary trainer (lib/dictBuilder/fastcover.c, FASTCOVER_ctx_init). It checks that all samples together are at least 8 bytes, but trains on the first 75% of the samples only (splitPoint 0.75). When that training part holds fewer than 8 bytes, nbDmers = trainingSamplesSize - 8 + 1 underflows and the trainer reads far past the sample buffer. Reported upstream as meta/zstd#4827, with a pure C reproduction.

Train already rejects zero length samples with ArgumentException, which prevents the easiest trigger, but any positive lengths whose first 75% sum to fewer than 8 bytes still reach the native code. Since a managed caller cannot catch an access violation, this is a process crash from data that may come from outside the application.

Reproduction Steps

repro.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
  </PropertyGroup>
</Project>

Program.cs

using System.IO.Compression;

// seven samples: six of one byte and one of 100 bytes, 106 bytes in total
int[] lengths = [1, 1, 1, 1, 1, 1, 100];
var samples = new byte[106];

using var dictionary = ZstandardDictionary.Train(samples, lengths, 256);
Console.WriteLine($"dictionary of {dictionary.Data.Length} bytes");
dotnet run -c Release

The content of the samples does not matter. What matters is that the first (int)(count * 0.75) sample lengths (here the first 5, which are 5 bytes) sum to fewer than 8 bytes while all samples sum to 8 bytes or more.

Expected behavior

Train fails with an exception, the same way it does for other sample sets the trainer rejects (for example too few samples), or returns a dictionary. zstd reports this class of input as srcSize_wrong when its check works.

Actual behavior

The process terminates:

Fatal error.
0xC0000005
   at System.IO.Compression.ZstandardDictionary.Train(System.ReadOnlySpan`1<Byte>, System.ReadOnlySpan`1<Int32>, Int32)
   at Program.<Main>$(System.String[])

Exit code 139. No managed exception is raised.

Regression?

No. ZstandardDictionary is new in .NET 11, there is no earlier release to compare with.

Known Workarounds

Validate the sample lengths before calling Train. The condition that triggers the crash is

static bool WouldCrash(ReadOnlySpan<int> sampleLengths)
{
    var trainingSamples = (int)(sampleLengths.Length * 0.75); // zstd's default split point
    long trainingTotal = 0;
    for (int i = 0; i < trainingSamples; i++) trainingTotal += sampleLengths[i];
    return trainingTotal < 8; // MAX(d, sizeof(U64)) with d = 8
}

Sample sets that small produce no useful dictionary anyway, so rejecting them is safe. A check like this inside Train would also be a reasonable fix on the .NET side until zstd is updated, since it only mirrors the check zstd intended to make.

Configuration

  • .NET 11.0.0 (SDK 11.0.100-rc.1.26425.128), System.IO.Compression from the shared framework.
  • Windows 11 Pro, 10.0.26200.
  • x64.
  • Not specific to this configuration. The same input crashes zstd 1.5.7 built from source with clang on Windows (see the upstream issue), and the faulty arithmetic is platform independent.
  • Not Blazor.

Other information

No response

Activity

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

    @dotnet-policy-service
    Contributor

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

  3. dotnet-policy-service commented on Oct 6, 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.

  4. added this to the 12.0.0 milestone on Oct 6, 2026
  5. removed
    untriaged
    on Oct 6, 2026
  6. added theissue type on Oct 6, 2026
  7. self-assigned this
    on Oct 6, 2026
  8. added a commit that references this issue on Oct 8, 2026
    1df8675
  9. locked and limited conversation to collaborators on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions