Repository navigation
Description
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.Compressionfrom 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
dotnet-policy-service commentedon Oct 6, 2026
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 6, 2026
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.
Prevent ZstandardDictionary.Train crashes for small sample sets (#135274