From a76d96e7308614b422508679d6905cbe1a8762bf Mon Sep 17 00:00:00 2001 From: Brotli Date: Fri, 12 Apr 2024 06:50:04 -0700 Subject: Don't check `cur_ix_masked` against `ring_buffer_mask`. `cur_ix_masked` isn't changing from iteration to iteration, and `max_length` ensures we never find a match long enough to walk off the ring buffer. PiperOrigin-RevId: 624162764 --- c/enc/hash.h | 6 +++--- c/enc/hash_forgetful_chain_inc.h | 4 ++-- c/enc/hash_longest_match64_inc.h | 7 +++---- c/enc/hash_longest_match_inc.h | 7 +++---- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/c/enc/hash.h b/c/enc/hash.h index ba9b0d8..f3a915b 100644 --- a/c/enc/hash.h +++ b/c/enc/hash.h @@ -557,6 +557,7 @@ static BROTLI_INLINE void FindCompoundDictionaryMatch( offset = distance_offset - distance; limit = source_size - offset; limit = limit > max_length ? max_length : limit; + BROTLI_DCHECK(cur_ix_masked + limit <= ring_buffer_mask) len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked], limit); if (len >= 2) { @@ -591,7 +592,7 @@ static BROTLI_INLINE void FindCompoundDictionaryMatch( limit = source_size - offset; limit = (limit > max_length) ? max_length : limit; if (distance > max_distance) continue; - if (cur_ix_masked + best_len > ring_buffer_mask || best_len >= limit || + if (best_len >= limit || /* compare 4 bytes ending at best_len + 1 */ BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != BrotliUnalignedRead32(&source[offset + best_len - 3])) { @@ -670,8 +671,7 @@ static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches( limit = source_size - offset; limit = (limit > max_length) ? max_length : limit; if (distance > max_distance) continue; - if (cur_ix_masked + best_len > ring_buffer_mask || - best_len >= limit || + if (best_len >= limit || data[cur_ix_masked + best_len] != source[offset + best_len]) { continue; } diff --git a/c/enc/hash_forgetful_chain_inc.h b/c/enc/hash_forgetful_chain_inc.h index 9a8efeb..17f31fa 100644 --- a/c/enc/hash_forgetful_chain_inc.h +++ b/c/enc/hash_forgetful_chain_inc.h @@ -203,6 +203,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( uint8_t* BROTLI_RESTRICT tiny_hashes = FN(TinyHash)(self->extra[0]); FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]); const size_t cur_ix_masked = cur_ix & ring_buffer_mask; + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask) /* Don't accept a short copy from far away. */ score_t min_score = out->score; score_t best_score = out->score; @@ -260,8 +261,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( prev_ix = (cur_ix - backward) & ring_buffer_mask; slot = banks[bank].slots[last].next; delta = banks[bank].slots[last].delta; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || /* compare 4 bytes ending at best_len + 1 */ BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) { diff --git a/c/enc/hash_longest_match64_inc.h b/c/enc/hash_longest_match64_inc.h index 8f825de..9834c2a 100644 --- a/c/enc/hash_longest_match64_inc.h +++ b/c/enc/hash_longest_match64_inc.h @@ -165,6 +165,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( uint16_t* BROTLI_RESTRICT num = self->num_; uint32_t* BROTLI_RESTRICT buckets = self->buckets_; const size_t cur_ix_masked = cur_ix & ring_buffer_mask; + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask) /* Don't accept a short copy from far away. */ score_t min_score = out->score; score_t best_score = out->score; @@ -184,8 +185,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } @@ -233,8 +233,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( break; } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || /* compare 4 bytes ending at best_len + 1 */ BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) { diff --git a/c/enc/hash_longest_match_inc.h b/c/enc/hash_longest_match_inc.h index c25cdb0..42b9532 100644 --- a/c/enc/hash_longest_match_inc.h +++ b/c/enc/hash_longest_match_inc.h @@ -164,6 +164,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( uint16_t* BROTLI_RESTRICT num = self->num_; uint32_t* BROTLI_RESTRICT buckets = self->buckets_; const size_t cur_ix_masked = cur_ix & ring_buffer_mask; + BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask) /* Don't accept a short copy from far away. */ score_t min_score = out->score; score_t best_score = out->score; @@ -183,8 +184,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } @@ -228,8 +228,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( break; } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || /* compare 4 bytes ending at best_len + 1 */ BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) != BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) { -- cgit v1.1