Skip to content

huf: limit Huffman codes to 11 bits, as the Zstandard format requires - #5104

Open
terrelln wants to merge 1 commit into
meta:devfrom
terrelln:2026-12-02-huffman-max-11-bits
Open

terrelln wants to merge 1 commit into
meta:devfrom
terrelln:2026-12-02-huffman-max-11-bits

Conversation

@terrelln

@terrelln terrelln commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

RFC 8878, section 4.2.1 limits the maximum code length to 11 bits, but HUF_TABLELOG_MAX was 12, so the decoder accepted Huffman tables with 12-bit codes, and so did the compressor when loading a dictionary.

Set HUF_TABLELOG_MAX to 11, which libzstd's compressor and dictionary builder already use, so one limit is enforced for frames and dictionaries, for decompression and compression. Decoding tables keep their capacity of HUF_TABLELOG_ABSOLUTEMAX (12), so the decoders are otherwise unchanged.

No release from v1.0.0 to v1.6.0 produces a table deeper than 11 bits. Two test inputs that were not made by libzstd had 12-bit tables, and now use 11 bits: test_optimalParserDictEntropy() and the http-dict-missing-symbols dictionary.

Add tests for 11- and 12-bit trees, and document the change in doc/decompressor_permissive.md.

@meta-cla meta-cla Bot added the CLA Signed label Dec 3, 2026
@terrelln
terrelln force-pushed the 2026-12-02-huffman-max-11-bits branch from ccf0f97 to b035490 Compare December 3, 2026 17:27
@terrelln

terrelln commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Ok, looks like the v0.7 dictionary builder emitted dictionaries in the v1.0 dictionary format which used a Huffman table log of 12. So if we started rejecting this entirely, we'd break these dictionaries, if any such dictionaries even exist.

RFC 8878, section 4.2.1 limits the maximum code length to 11 bits, but
HUF_TABLELOG_MAX was 12, so the decoder accepted Huffman tables with 12-bit
codes, and so did the compressor when loading a dictionary.

Set HUF_TABLELOG_MAX to 11, which libzstd's compressor and dictionary builder
already use, so one limit is enforced for frames and dictionaries, for
decompression and compression. Decoding tables keep their capacity of
HUF_TABLELOG_ABSOLUTEMAX (12), so the decoders are otherwise unchanged.

No release from v1.0.0 to v1.6.0 produces a table deeper than 11 bits. Two test
inputs that were not made by libzstd had 12-bit tables, and now use 11 bits:
test_optimalParserDictEntropy() and the http-dict-missing-symbols dictionary.

Add tests for 11- and 12-bit trees, and document the change in
doc/decompressor_permissive.md.
@terrelln
terrelln force-pushed the 2026-12-02-huffman-max-11-bits branch from b035490 to a6cfe80 Compare December 4, 2026 21:49

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