diff --git a/c/enc/backward_references_hq.c b/c/enc/backward_references_hq.c index 6325032e1..9415fab06 100644 --- a/c/enc/backward_references_hq.c +++ b/c/enc/backward_references_hq.c @@ -435,6 +435,8 @@ static size_t UpdateNodes( const CompoundDictionary* addon = ¶ms->dictionary.compound; size_t gap = addon->total_size; + BROTLI_DCHECK(cur_ix_masked + max_length <= ringbuffer_mask); + EvaluateNode(block_start + stream_offset, pos, max_backward_limit, gap, starting_dist_cache, model, queue, nodes); diff --git a/c/enc/hash.h b/c/enc/hash.h index ba9b0d820..9533b05be 100644 --- a/c/enc/hash.h +++ b/c/enc/hash.h @@ -548,6 +548,8 @@ static BROTLI_INLINE void FindCompoundDictionaryMatch( source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); } + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + for (i = 0; i < 4; ++i) { const size_t distance = (size_t)distance_cache[i]; size_t offset; @@ -657,6 +659,8 @@ static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches( source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail); } + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + while (item == 0) { size_t offset; size_t distance; diff --git a/c/enc/hash_forgetful_chain_inc.h b/c/enc/hash_forgetful_chain_inc.h index 9a8efeb6c..c44276fa4 100644 --- a/c/enc/hash_forgetful_chain_inc.h +++ b/c/enc/hash_forgetful_chain_inc.h @@ -212,6 +212,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)( const uint8_t tiny_hash = (uint8_t)(key); out->len = 0; out->len_code_delta = 0; + + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + /* Try last distance first. */ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) { const size_t backward = (size_t)distance_cache[i]; diff --git a/c/enc/hash_longest_match64_inc.h b/c/enc/hash_longest_match64_inc.h index 8f825de6f..441ab86b4 100644 --- a/c/enc/hash_longest_match64_inc.h +++ b/c/enc/hash_longest_match64_inc.h @@ -172,6 +172,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)( size_t i; out->len = 0; out->len_code_delta = 0; + + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + /* Try last distance first. */ for (i = 0; i < (size_t)self->num_last_distances_to_check_; ++i) { const size_t backward = (size_t)distance_cache[i]; diff --git a/c/enc/hash_longest_match_inc.h b/c/enc/hash_longest_match_inc.h index c25cdb0b9..4679dac39 100644 --- a/c/enc/hash_longest_match_inc.h +++ b/c/enc/hash_longest_match_inc.h @@ -169,6 +169,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)( score_t best_score = out->score; size_t best_len = out->len; size_t i; + + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + out->len = 0; out->len_code_delta = 0; /* Try last distance first. */ diff --git a/c/enc/hash_longest_match_quickly_inc.h b/c/enc/hash_longest_match_quickly_inc.h index 1f3602284..57ed586fe 100644 --- a/c/enc/hash_longest_match_quickly_inc.h +++ b/c/enc/hash_longest_match_quickly_inc.h @@ -164,6 +164,9 @@ static BROTLI_INLINE void FN(FindLongestMatch)( size_t best_len = best_len_in; size_t cached_backward = (size_t)distance_cache[0]; size_t prev_ix = cur_ix - cached_backward; + + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask); + out->len_code_delta = 0; if (prev_ix < cur_ix) { prev_ix &= (uint32_t)ring_buffer_mask;