Skip to content

Commit

Permalink
Fix a bug in ZLIB_DEBUG compiles in check_match().
Browse files Browse the repository at this point in the history
This avoids trying to compare a match starting one byte before the
current window. Thanks to @zmodem (Hans) for discovering this.
  • Loading branch information
madler committed Jan 19, 2024
1 parent 7b632b4 commit 7af6320
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,13 +1556,21 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
*/
local void check_match(deflate_state *s, IPos start, IPos match, int length) {
/* check that the match is indeed a match */
if (zmemcmp(s->window + match,
s->window + start, length) != EQUAL) {
fprintf(stderr, " start %u, match %u, length %d\n",
start, match, length);
Bytef *back = s->window + (int)match, *here = s->window + start;
IPos len = length;
if (match == (IPos)-1) {
/* match starts one byte before the current window -- just compare the
subsequent length-1 bytes */
back++;
here++;
len--;
}
if (zmemcmp(back, here, len) != EQUAL) {
fprintf(stderr, " start %u, match %d, length %d\n",
start, (int)match, length);
do {
fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
} while (--length != 0);
fprintf(stderr, "(%02x %02x)", *back++, *here++);
} while (--len != 0);
z_error("invalid match");
}
if (z_verbose > 1) {
Expand Down

0 comments on commit 7af6320

Please sign in to comment.