소스 검색

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.
Nico Weber 1 년 전
부모
커밋
2023e8d8d9
1개의 변경된 파일3개의 추가작업 그리고 1개의 파일을 삭제
  1. 3 1
      Userland/Libraries/LibCompress/Huffman.h

+ 3 - 1
Userland/Libraries/LibCompress/Huffman.h

@@ -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;