Repository navigation
Conversation
`HuffmanTable::new_optimized` follows T.81 K.2-K.4 literally, which sizes the code length histogram at 33 and sorts symbols by sizes 1..=32. Skewed counts build deeper trees than that: 33 symbols with tie-free near-Fibonacci counts (f(n) = f(n-1) + f(n-2) + 1, ~18.5M total) index past `bits[33]` and panic in debug and release builds. Exact Fibonacci counts do not trigger it, because the K.1 tie-breaking rebalances them. Size the histogram for the deepest tree 257 symbols can form and sort over every size, so K.3 limits any depth to 16 bits. Also merge counts in u64: `freq[v1] += freq[v2]` overflowed (debug panic) once a table's total count exceeded u32::MAX. Adds proptest as a dev-dependency for property tests of the generated tables (lengths sum to the value count, Kraft sum below 1 so the all-ones code stays reserved, every used symbol has a code of 1..=16 bits, codes are prefix-free), plus the two failing cases as fixed tests.
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.
HuffmanTable::new_optimizedfollows T.81 K.2–K.4 literally: the code-length histogrambitshas 33 entries, and the K.4 sort only visits sizes1..=32. Skewed symbol counts build deeper trees than that. With 33 symbols whose counts follow a tie-free near-Fibonacci sequence (f(n) = f(n-1) + f(n-2) + 1, about 18.5M symbols in total, i.e. a large image with adversarial content), K.1 produces codes longer than 32 bits andbits[size] += 1indexes out of bounds. That panics in both debug and release builds. libjpeg reports an error in this case.An exact Fibonacci sequence does not trigger it, because the K.1 tie-breaking rebalances the tree.
Separately,
freq[v1] += freq[v2]overflows (debug panic) once a table's total count exceedsu32::MAX.Change
u64(freq.map(u64::from)).bitsfor the deepest tree 257 symbols can form ([0u16; 257]), start K.3 at the last index and sort over every size in K.4. K.3 then limits any depth to 16 bits, as intended.Tests
This adds
proptestas a dev-dependency (it builds and passes on 1.89). The newoptimized_propsmodule checks every generated table for:sum(length) == values.len()These are run over uniform and heavily skewed AC counts and arbitrary DC counts. Two fixed cases,
fibonacci_depthandtotal_over_u32, fail onmainand pass with the fix.cargo fmt --checkandcargo testpass with--features simd, with--features use_wide, with--no-default-features --tests, and in--release, also on 1.89.