From 132445a5e6953a5adf0727dfa78d1eb2b02dea83 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 01:26:30 +0000 Subject: [PATCH 1/2] script: keep metablock parity harness distances valid Clamp the synthetic commands' distance codes to the distance params' max_distance, as the encoder does. Larger (invalid) distances made the C code's ComputeDistanceCost write past the end of its distance histogram, which showed up as a spurious mismatch for a large-window configuration. --- script/manual-test-brotli-metablock-parity.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/script/manual-test-brotli-metablock-parity.c b/script/manual-test-brotli-metablock-parity.c index 7f3c7443..baa3e58f 100644 --- a/script/manual-test-brotli-metablock-parity.c +++ b/script/manual-test-brotli-metablock-parity.c @@ -455,6 +455,14 @@ static size_t // random_distance(block_t* b) { uint32_t r = rng_below(16); size_t max = b->large_window ? 0x7FFFFFFC : ((1u << 24) - 16); + // Keep the distance codes (distance + 15) at most the distance params' + // max_distance (which is less than 0x7FFFFFFC for some large-window + // params), as the encoder does. Larger distances are invalid for those + // params and make the C code's ComputeDistanceCost write past the end of + // its distance histogram. + if (max > (b->dist.max_distance - 15)) { + max = b->dist.max_distance - 15; + } if ((g_dist_style == 1) && (r < 12)) { // Distances with a fixed residue modulo (1 << g_dist_shift), which // favors npostfix > 0. From 1bd0d84299ee3ad6b223a440e38edeeda85a8780 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 27 Oct 2026 01:26:46 +0000 Subject: [PATCH 2/2] std/brotli: document build_meta_block's distance precondition --- std/brotli/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/std/brotli/README.md b/std/brotli/README.md index 25e0a3a7..3ded5404 100644 --- a/std/brotli/README.md +++ b/std/brotli/README.md @@ -394,7 +394,12 @@ The exact signatures are: bytes. `literal_context_mode` is a `ContextType` (0 = LSB6, 1 = MSB6, 2 = UTF8, 3 = SIGNED). Like the C code, it rewrites the commands' `dist_prefix_` and `dist_extra_` fields for the chosen distance params and - leaves those params in the `dist_etc` fields. It returns + leaves those params in the `dist_etc` fields. Like the encoder's commands, + every command's distance code must be at most the given params' + `max_distance` (with larger, invalid distance codes, the C code's + `ComputeDistanceCost` writes past the end of its distance histogram, which + the Wuffs code cannot mirror: it ignores out-of-range histogram symbols + instead). It returns `"#bad workbuf length"` if `wb` is too short (the results are then invalid but `cmds` may already be rewritten, so retrying needs the original commands).