Commit 7a6556a
fix: resolve potential infinite loop and panic in Huffman table generation
This commit addresses a test hang observed in `tests/offset_tests.rs` (specifically `test_offset_12_pattern`) by hardening the Huffman table generation logic in `src/decompress/mod.rs`.
Changes:
- **Fix `bsr32` Underflow:** Added a check for `diff == 0` before calling `bsr32` in the canonical codeword generation loop. When the table is fully utilized for a given bit length, `codeword ^ mask` can be 0, causing `bsr32` (implemented as `31 - leading_zeros`) to underflow/panic in debug builds or produce undefined shifts. The logic now correctly resets `codeword` to 0 in this edge case (wrapping around).
- **Add Loop Guards:** Introduced explicit iteration limits to the nested loops in `build_decode_table`. If the loops exceed safe bounds (indicating an infinite loop due to invalid Huffman data or logic errors), the function now returns `false` (error) instead of hanging indefinitely. This prevents the process from getting stuck.
This ensures robust handling of edge-case Huffman trees and prevents both crashes and hangs during table construction.
Co-authored-by: 404Setup <[email protected]>1 parent d1ccdb2 commit 7a6556a
1 file changed
Lines changed: 18 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1776 | 1776 | | |
1777 | 1777 | | |
1778 | 1778 | | |
| 1779 | + | |
1779 | 1780 | | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
1780 | 1785 | | |
| 1786 | + | |
1781 | 1787 | | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
1782 | 1792 | | |
1783 | 1793 | | |
1784 | 1794 | | |
| |||
1792 | 1802 | | |
1793 | 1803 | | |
1794 | 1804 | | |
1795 | | - | |
1796 | | - | |
1797 | | - | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
1798 | 1813 | | |
1799 | 1814 | | |
1800 | 1815 | | |
| |||
0 commit comments