Skip to content

Prevent ZstandardDictionary.Train crashes for small sample sets - #135274

Merged
rzikm merged 1 commit into
dotnet:mainfrom
rzikm:rzikm/zstandard-training-crash
Oct 8, 2026
Merged

rzikm merged 1 commit into
dotnet:mainfrom
rzikm:rzikm/zstandard-training-crash

Conversation

@rzikm

@rzikm rzikm commented Oct 6, 2026

Copy link
Copy Markdown
Member

ZstandardDictionary.Train can crash the process when the first 75% of samples contain fewer than 8 bytes. The bundled zstd trainer validates the total corpus size, but computes its working size from that smaller training subset, allowing unsigned underflow.

Add a temporary managed guard that sums the training prefix during the existing sample-length validation and copy. Reject insufficient training data through the existing srcSize_wrong error mapping to IOException, preserving argument-validation order. Vendored zstd remains unchanged; the workaround can be removed once the bundled version includes a fix for facebook/zstd#4827.

Regression coverage includes the reported crash, different sample counts and prefix sizes, successful 8-byte and 9-byte boundaries, and argument-validation precedence.

Validation

Executed on Windows x64 using a dedicated subst path:

  • .\build.cmd clr+libs -rc release: passed.
  • .\.dotnet\dotnet.exe build src\libraries\System.IO.Compression\tests\System.IO.Compression.Tests.csproj /t:Test /p:XunitMethodName=System.IO.Compression.ZstandardDictionaryTests.Train_InsufficientTrainingBytes_ThrowsIOException: reproduced the access violation before the fix (0xC0000005).
  • U:\.dotnet\dotnet.exe build from src\libraries\System.IO.Compression: passed.
  • U:\.dotnet\dotnet.exe build tests\System.IO.Compression.Tests.csproj /t:Test /p:XunitClassName=System.IO.Compression.ZstandardDictionaryTests from that directory: passed, 17 tests.
  • .\.dotnet\dotnet.exe build src\libraries\System.IO.Compression\tests\System.IO.Compression.Tests.csproj /t:Test: passed on the final patch, 2,999 tests with zero failures.

Other platforms and outer-loop tests were not run.

Resolves #135264

Note

This pull request description and code changes were generated with GitHub Copilot.

Guard the native trainer's 75% sample prefix against size underflow while preserving existing IOException mapping and argument validation. Add crash regressions and minimum-size boundary coverage.

Co-authored-by: Copilot App <[email protected]>
@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.

@rzikm
rzikm requested a review from a team October 7, 2026 09:12
@rzikm
rzikm merged commit 1df8675 into dotnet:main Oct 8, 2026
87 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants