LZIP header encodes smaller dictionary than writer uses

perfloop-oss/lzma-rust2 · Numeric Representation Loss

https://perfloop.ai/t/oss/case_jjgagzvctr

Verdict

VERIFIED · settled 2026-11-25 · pull request opened as hasenbanck/lzma-rust2#152

What happened: The check fails before this change and passes with it.

Hypothesis

When writing an LZIP member header, the dictionary size encoder rounds the subtracted fraction upward instead of downward. Because that fraction is subtracted from the next power of two, rounding upward shrinks the dictionary size recorded in the header below the dictionary size the compressor actually uses. Any back-reference emitted between the header's smaller dictionary size and the compressor's real dictionary size makes the output unreadable, and valid dictionary sizes between 256 MiB and 288 MiB fail outright with an error.

In `src/lzip.rs`, `encode_dict_size` picks the smallest power of two `base_size = 1 << base_log2 >= dict_size`, sets `diff = base_size - dict_size` and `fraction_unit = base_size >> 4`, and computes `fraction_num = diff.div_ceil(fraction_unit)` at line 163. Because `base_size / 2 < dict_size <= base_size`, `diff` is strictly less than `8 * fraction_unit`: - Whenever `diff` is not a multiple of `fraction_unit` and `diff <= 7 * fraction_unit`, `div_ceil` increases `fraction_num` by 1, so `decode_dict_size(encoded) = base_size - fraction_num * fraction_unit < dict_size`. `LzipWriter::start_new_member` (and worker threads in `LzipWriterMt`) writes this header byte and then constructs `LzmaWriter::new_no_header` with the larger `self.options.lzma_options.dict_size`, allowing match distances that exceed the header's dictionary and causing `LzipReader` (and any spec-compliant LZIP decoder) to fail with `dist overflow`. - Whenever `7 * fraction_unit < diff < 8 * fraction_unit` (that is, `base_size / 2 < dict_size < 9/16 * base_size`), `div_ceil` yields `8 > 7`, bumping `base_log2` by 1. For `dict_size` in `(256 MiB, 288 MiB)` (`base_log2 == 29`), `base_log2` becomes `30 > 29` and `encode_dict_size` returns `Err(InvalidInput("dictionary size too large"))` even though `LzipWriter::new` explicitly accepts and clamps `dict_size` into `MIN_DICT_SIZE..=MAX_DICT_SIZE` (`4 KiB..=512 MiB`).

A bounded sweep across 122 boundary dictionary sizes in `4 KiB..=512 MiB` produced 35 round-trip decompression failures (`dist overflow`, starting at `dict_size = 5119` encoding header byte `0xed` = 4608 bytes) and failed `finish()` on `268_435_457` and `301_989_887`. The attached deterministic test `cargo test --test lzip_dict_size_roundtrip` reproduces both failures on the current revision and passes when `diff.div_ceil(fraction_unit)` is replaced with `diff / fraction_unit`. The Case should verify that for every `dict_size` in `4 KiB..=512 MiB`, `encode_dict_size` succeeds, `decode_dict_size(encode_dict_size(d)) >= d`, round-trip decoding succeeds for both `LzipWriter` and `LzipWriterMt`, and `encode_dict_size(decode_dict_size(b)) == b` holds for all valid encoded bytes.

Upstream check: `hasenbanck/lzma-rust2` currently has no open pull requests (`upstreamWork(kind="open")` returned empty), and no prior finding targets `encode_dict_size`.

Change to test: Compute the subtracted fraction with truncating division (`diff / fraction_unit`) instead of `div_ceil` in `encode_dict_size`, removing the dead `fraction_num > 7` fallback branch. This guarantees every supported dictionary size in 4 KiB..=512 MiB encodes to the smallest representable LZIP header dictionary size at least as large as the writer's dictionary.

Where it lives

perfloop-oss/lzma-rust2 · src/lzip/writer.rs

Evidence

The check fails before this change and passes with it: `For every dictionary size from 4096 through 536870912 bytes, the LZIP header encoder succeeds and decoding its output yields at least the requested size; re-encoding every valid header dictionary byte returns that byte.`

The check fails before this change and passes with it: `With a 5119-byte dictionary and preset 0 otherwise unchanged, both LzipWriter and LzipWriterMt compress tests/data/pg100.txt into a member from which both LzipReader and LzipReaderMt recover every original byte without error.`

Checks: 8 of 8 passed. Verification: no defect found.

Timeline