fix: reject incomplete Huffman trees with max code length > 1#190
Merged
oyvindln merged 1 commit intoFrommi:masterfrom Feb 23, 2026
Merged
fix: reject incomplete Huffman trees with max code length > 1#190oyvindln merged 1 commit intoFrommi:masterfrom
oyvindln merged 1 commit intoFrommi:masterfrom
Conversation
Fixes Frommi#137. The previous validation only rejected incomplete Huffman trees when more than one symbol had a non-zero code length. This allowed single-symbol trees with code lengths > 1 that don't form valid prefix codes to pass validation, even though zlib rejects them. The fix replaces the `used_symbols > 1` check with early over-subscription detection (left < 0 at each step) and an incomplete-tree check matching zlib's inftrees.c logic: - Code length (hufflen) tables must always be complete - Literal/length and distance tables allow incomplete codes only when max_code_len <= 1 (all symbols unused, or a single symbol with a 1-bit code like an EOB-only block) Includes regression test with the exact vector from issue Frommi#137.
Collaborator
|
Seems good Thanks for fixing this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #137.
The previous Huffman tree validation in
init_treeonly rejected incomplete trees whenused_symbols > 1. This allowed a single symbol with a code length > 1 (e.g., code length 2 or higher) through validation, even though such a tree doesn't form a valid prefix code. zlib rejects these streams; miniz_oxide accepted them.Changes
Over-subscription detection: Added an early
left < 0check at each code length step, matching zlib'sinftrees.clogic. The previous code only checked completeness after the loop, missing cases where the tree was over-subscribed partway through.Incomplete tree rejection: Replaced the
used_symbols > 1condition withmax_code_len > 1, matching zlib'smax != 1check:max_code_len <= 1:max_code_len == 0: all symbols unused (e.g., a distance table with no backreferences)max_code_len == 1: a single symbol with a 1-bit code (e.g., an EOB-only litlen table)max_code_len > 1with an incomplete tree → rejectedReference
The fix matches zlib's
inftrees.clines 126-133:Test plan
issue_137_reject_incomplete_litlen_treewith exact vector from miniz_oxide accepts invalid literal/length trees #137decompress_empty_dynamicwhich exercises valid single-symbol incomplete tables)zlib.decompress()) and the fixed miniz_oxide