From c7b08c52826701efef7e5e99acb180e0c78aa572 Mon Sep 17 00:00:00 2001 From: Jeff Lenamon <85593689+lenamonj@users.noreply.github.com> Date: Mon, 7 Sep 2026 00:03:12 -0400 Subject: [PATCH] Fix index out of bounds under the disallow_large_window_size feature BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS was defined twice: unconditionally 544 in enc/constants.rs and privately in enc/histogram.rs, where only that copy shrank to 520 under disallow_large_window_size. The histogram array took the private value while every bound took the public one, so the encoder indexed one past the end. Keep one feature-aware definition. --- src/enc/constants.rs | 3 +++ src/enc/histogram.rs | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/enc/constants.rs b/src/enc/constants.rs index 4e19756b..2453386f 100644 --- a/src/enc/constants.rs +++ b/src/enc/constants.rs @@ -104,7 +104,10 @@ static kContextLookup: [u8; 2048] = [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, ]; +#[cfg(not(feature = "disallow_large_window_size"))] pub const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS: usize = 544; +#[cfg(feature = "disallow_large_window_size")] +pub const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS: usize = 520; pub const BROTLI_NUM_LITERAL_SYMBOLS: usize = 256; pub const BROTLI_NUM_COMMAND_SYMBOLS: usize = 704; pub const BROTLI_WINDOW_GAP: usize = 16; diff --git a/src/enc/histogram.rs b/src/enc/histogram.rs index 3825ccbd..30cb03b2 100644 --- a/src/enc/histogram.rs +++ b/src/enc/histogram.rs @@ -5,7 +5,9 @@ use super::super::alloc; use super::super::alloc::{SliceWrapper, SliceWrapperMut}; use super::block_split::BlockSplit; use super::command::Command; -use super::constants::{kSigned3BitContextLookup, kUTF8ContextLookup}; +use super::constants::{ + kSigned3BitContextLookup, kUTF8ContextLookup, BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS, +}; use super::util::floatX; use super::vectorization::Mem256i; @@ -63,11 +65,6 @@ impl Default for HistogramCommand { } //#[derive(Clone)] // #derive is broken for arrays > 32 -#[cfg(not(feature = "disallow_large_window_size"))] -const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS: usize = 544; -#[cfg(feature = "disallow_large_window_size")] -const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS: usize = 520; - pub struct HistogramDistance { pub data_: [u32; BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS], pub total_count_: usize,