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: 616440826
  • Loading branch information
Brotli authored and copybara-github committed Mar 17, 2024
1 parent 9717649 commit f919c06
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
6 changes: 2 additions & 4 deletions c/enc/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,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 ||
data[cur_ix_masked + best_len] != source[offset + best_len]) {
continue;
}
Expand Down Expand Up @@ -664,8 +663,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;
}
Expand Down
3 changes: 1 addition & 2 deletions c/enc/hash_forgetful_chain_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,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 ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions c/enc/hash_longest_match64_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,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;
}
Expand Down Expand Up @@ -228,8 +227,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 ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions c/enc/hash_longest_match_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,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;
}
Expand Down Expand Up @@ -221,8 +220,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 ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down

0 comments on commit f919c06

Please sign in to comment.