LibCompress: Use saturating add in generate_huffman_lengths()

For our deflate, block size is limited to less than 64 kiB, so the sum
of all byte frequencies always fits in a u16 by construction.

But while I haven't hit this in practice, but it can conceivably happen
when writing WebP files, which currently use a single huffman tree
(per channel) for a while image -- which is often much larger than
64 kiB.

No dramatic behavior change in practice, just feels more correct.
This commit is contained in:
Nico Weber 2024-05-23 19:10:45 -04:00 committed by Andreas Kling
parent 0711e9d749
commit 2023e8d8d9
Notes: sideshowbarker 2024-07-16 22:17:03 +09:00

View file

@ -53,7 +53,9 @@ void generate_huffman_lengths(Array<u8, Size>& lengths, Array<u16, Size> const&
u16 new_link = heap.size() + 1;
heap.insert(lowest_frequency + second_lowest_frequency, new_link);
u32 sum = lowest_frequency + second_lowest_frequency;
sum = min(sum, UINT16_MAX);
heap.insert(sum, new_link);
huffman_links[lowest_link] = new_link;
huffman_links[second_lowest_link] = new_link;