Skip to content

compress: respect ZSTD_c_forceMaxWindow with a copied CDict - #5068

Open
pylaterreur wants to merge 1 commit into
meta:devfrom
pylaterreur:bug-forcemaxwindow-cdict
Open

pylaterreur wants to merge 1 commit into
meta:devfrom
pylaterreur:bug-forcemaxwindow-cdict

Conversation

@pylaterreur

Copy link
Copy Markdown

What and why

ZSTD_c_forceMaxWindow is documented to "Force back-reference distances to remain < windowSize, even when referencing into Dictionary content". ZSTD_CCtx_loadDictionary() builds its CDict with the context's parameters, and ZSTD_CCtx_refPrefix() loads the prefix with them. In both cases, ZSTD_loadDictionaryContent() sets loadedDictEnd to 0 when forceWindow is set, so the window also bounds matches into the dictionary. A CDict referenced with ZSTD_CCtx_refCDict() is copied into the context when forceWindow is set, since attaching is then disabled (ZSTD_shouldAttachDict()), unless the CDict was built with ZSTD_c_enableDedicatedDictSearch, which is always attached. But ZSTD_resetCCtx_byCopyingCDict() copies the CDict's loadedDictEnd, which was computed with the CDict's own parameters, usually without forceWindow. The whole dictionary then stays reachable for the first window of input.

Example: 1 KB window (windowLog = 10), ZSTD_c_forceMaxWindow = 1, a 64 KB dictionary of random bytes, and an 8 KB source that repeats the start of the dictionary. The largest offset that ZSTD_generateSequences() reports is 0 with ZSTD_CCtx_loadDictionary() (871 at level 19), and 65536 with ZSTD_CCtx_refCDict(), at levels 1, 3, 6, 12 and 19. The format allows such references within the first Window_Size bytes of a frame, so the zstd decoder accepts these frames; it is the documented guarantee of the parameter that is not kept.

This change clears loadedDictEnd when forceWindow is set, as ZSTD_loadDictionaryContent() does. The new test in tests/fuzzer.c checks the offsets for that source with a referenced CDict. It fails on dev and passes with this change.

Not covered: a CDict built with ZSTD_c_enableDedicatedDictSearch is attached even with forceWindow, and still reaches 64 KB back at levels 5 and 6 in the example above. Attached dictionaries need a different fix, which I left out of this change.

Checklist

  • I searched open pull requests and issues: this problem is not already being addressed.
  • Bug fix: it comes with a reproducer or a test that fails without the fix.
  • Performance change: numbers from repeated runs are included, with CPU, compiler, flags and data.
    A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
  • I stated below whether AI or other automated tools were used.

Tools used

Claude Code (Anthropic's AI coding agent) found the problem while comparing the documentation of the dictionary parameters with their behaviour, and wrote the fix, the test and this description.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

ZSTD_c_forceMaxWindow is documented to keep back-reference distances
within the window, even into dictionary content. For a dictionary loaded
into the context, ZSTD_loadDictionaryContent() sets loadedDictEnd to 0
for that purpose, so the window also bounds matches into the dictionary.
A referenced CDict is copied into the context in that case (attaching is
disabled with forceWindow), but ZSTD_resetCCtx_byCopyingCDict() copied
the CDict's loadedDictEnd, computed without forceWindow. The whole
dictionary then stayed reachable for the first window of input: with a
1 KB window and a 64 KB CDict, ZSTD_generateSequences() reports offsets
of 65536.

Clear loadedDictEnd when forceWindow is set, as for a loaded dictionary.
Add a test that checks the offsets for a source that repeats the start
of a 64 KB CDict, with a 1 KB window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
@meta-cla meta-cla Bot added the CLA Signed label Nov 29, 2026

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant