You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lossless decoder: Huffman group count is unbounded by image size (memory amplification) #197
read_huffman_codes sets the number of Huffman code groups to the largest value in the entropy image plus one, and then reads and keeps every group. That count is not tied to the image size, so:
a 1x1 lossless image whose single entropy pixel is 0xFFFF declares 65,536 groups; a ~160 KB file makes read_image allocate about 18 MB of HuffmanTrees, all but one never used;
a 1024x1024 image with 4x4 blocks can legitimately use 65,536 distinct groups; with codes that build decoding tables (up to 1024 u32 entries, plus tree nodes for codes longer than 10 bits) this reaches gigabytes from a file of a few MB.
WebPDecoder::set_memory_limit does not apply to any of these allocations (only to metadata chunks), so a caller cannot bound it.
libwebp handles the first case in ReadHuffmanCodes (src/dec/vp8l_dec.c): when the declared group count is large or exceeds the number of blocks, it remaps the used group indices to a dense range and reads past the codes of unused groups instead of storing them. Real libwebp output declares unused groups too (we saw 9 of 46 streams declaring 3 or 4 groups and using 2 or 3).
We are carrying a patch against 0.2.4 that (1) remaps used groups like libwebp, so at most one group per block is kept, and (2) counts transform images, entropy images, color caches and kept Huffman trees against the decoder's memory_limit, returning DecodingError::MemoryLimitExceeded past it, for still images, animation frames and the lossless alpha plane. Decoded pixels are unchanged on our test set (106 files) and the crate's unit tests pass. Happy to open a PR against main if this approach suits you.
We ran into this independently while bounding WebP uploads in a service that decodes untrusted images, and we are carrying a patch against 0.2.4 that takes a complementary approach.
Instead of remapping unused groups, it makes set_memory_limit cover the decoder's own allocations. A MemoryBudget is charged before each allocation is made: Huffman code groups and their tables, the transform, color-cache and entropy images, the colour-indexing table, and the decoded pixel buffer. Past the limit, the decoder returns DecodingError::MemoryLimitExceeded. A single budget is shared across a frame, so the lossless alpha plane and the VP8 data in an animation frame draw on the same limit instead of each getting a fresh one. Charging per group before the tree is read also bounds the 65,536-group case you describe, from a small file, once a caller sets a limit.
The two approaches fit together. Remapping removes the waste for well-formed files that declare unused groups. The budget gives callers a hard ceiling for any input, including the legitimate 1024x1024 / 65,536-group case, which remapping alone cannot bound. The crate's unit tests pass with it, and it adds allocation-counting tests that assert the limit holds.
@Eoth, are you planning to open the PR? If you'd prefer to keep yours to the remapping, I'm happy to send the budget half as a separate PR on main once the decoder layout settles. Maintainers, let us know which shape you'd rather review.
We ran into this issue too and made a fix that also tested on our system. It remaps the Huffman groups used by the image and checks lossless, ALPH, VP8, and animation buffers against one memory limit. Unused groups are still checked, but their tables are not kept.
This may help solve the issue, the PR is here: #211
Thanks both. I won't open a separate PR: #211 covers both halves, remapping unused groups and a shared memory limit, which is what our patch did.
I tested #211 against main and against our patched 0.2.4 copy, measuring peak heap during decoding with a counting global allocator (input and output buffer excluded):
157 files (57 real-world WebP, plus 100 made with libwebp 1.6.0: lossless at methods 0 to 6, palettes, alpha, animations): decoded pixels identical on main, Bound memory use in lossless WebP decoding #211 and our copy, with and without a limit.
1024x1024 image using one group per block (all 65,536 used, 295 KB file, 22.7 MB on main): MemoryLimitExceeded under a 16 MiB limit, peak 4.7 MB.
Limits from 64 KiB to 128 MiB over all 161 files: peak never above the limit, except by 2 bytes in one case (limit exactly 4 MiB). No refusal when the limit is at least twice the unlimited peak.
cargo test passes.
Once this is in a release we will drop our patched copy.
Activity
daniel-pinder commentedon Nov 12, 2026
We ran into this independently while bounding WebP uploads in a service that decodes untrusted images, and we are carrying a patch against 0.2.4 that takes a complementary approach.
Instead of remapping unused groups, it makes
set_memory_limitcover the decoder's own allocations. AMemoryBudgetis charged before each allocation is made: Huffman code groups and their tables, the transform, color-cache and entropy images, the colour-indexing table, and the decoded pixel buffer. Past the limit, the decoder returnsDecodingError::MemoryLimitExceeded. A single budget is shared across a frame, so the lossless alpha plane and the VP8 data in an animation frame draw on the same limit instead of each getting a fresh one. Charging per group before the tree is read also bounds the 65,536-group case you describe, from a small file, once a caller sets a limit.The two approaches fit together. Remapping removes the waste for well-formed files that declare unused groups. The budget gives callers a hard ceiling for any input, including the legitimate 1024x1024 / 65,536-group case, which remapping alone cannot bound. The crate's unit tests pass with it, and it adds allocation-counting tests that assert the limit holds.
@Eoth, are you planning to open the PR? If you'd prefer to keep yours to the remapping, I'm happy to send the budget half as a separate PR on
mainonce the decoder layout settles. Maintainers, let us know which shape you'd rather review.advolkov commentedon Nov 12, 2026
We ran into this issue too and made a fix that also tested on our system. It remaps the Huffman groups used by the image and checks lossless, ALPH, VP8, and animation buffers against one memory limit. Unused groups are still checked, but their tables are not kept.
This may help solve the issue, the PR is here: #211
Eoth commentedon Nov 12, 2026
Thanks both. I won't open a separate PR: #211 covers both halves, remapping unused groups and a shared memory limit, which is what our patch did.
I tested #211 against main and against our patched 0.2.4 copy, measuring peak heap during decoding with a counting global allocator (input and output buffer excluded):
Once this is in a release we will drop our patched copy.
advolkov commentedon Nov 18, 2026
Any chance to move forward with #211?