Skip to content

Commit

Permalink
Don't check cur_ix_masked against ring_buffer_mask.
Browse files Browse the repository at this point in the history
`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: 624701948
  • Loading branch information
eustas authored and copybara-github committed Apr 14, 2024
1 parent 709c467 commit c1c76e9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions c/enc/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ 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) {
Expand Down Expand Up @@ -592,7 +591,7 @@ static BROTLI_INLINE void FindCompoundDictionaryMatch(
limit = source_size - offset;
limit = (limit > max_length) ? max_length : limit;
if (distance > max_distance) continue;
if (best_len >= limit ||
if (cur_ix_masked + best_len > ring_buffer_mask || 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])) {
Expand Down Expand Up @@ -671,7 +670,8 @@ static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches(
limit = source_size - offset;
limit = (limit > max_length) ? max_length : limit;
if (distance > max_distance) continue;
if (best_len >= limit ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
best_len >= limit ||
data[cur_ix_masked + best_len] != source[offset + best_len]) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions c/enc/hash_forgetful_chain_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ 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);
const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
const uint8_t tiny_hash = (uint8_t)(key);
out->len = 0;
Expand Down Expand Up @@ -261,7 +260,8 @@ 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 (prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
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])) {
Expand Down
7 changes: 4 additions & 3 deletions c/enc/hash_longest_match64_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ 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. */
Expand All @@ -185,7 +184,8 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -233,7 +233,8 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
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])) {
Expand Down
7 changes: 4 additions & 3 deletions c/enc/hash_longest_match_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ 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. */
Expand All @@ -184,7 +183,8 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -228,7 +228,8 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask ||
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])) {
Expand Down

0 comments on commit c1c76e9

Please sign in to comment.