Fix a bug in ZLIB_DEBUG compiles in check_match().

This avoids trying to compare a match starting one byte before the
current window. Thanks to @zmodem (Hans) for discovering this.
This commit is contained in:
Mark Adler 2024-01-19 12:19:53 -08:00
parent 7b632b486a
commit 7af6320ad7

View File

@ -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) { local void check_match(deflate_state *s, IPos start, IPos match, int length) {
/* check that the match is indeed a match */ /* check that the match is indeed a match */
if (zmemcmp(s->window + match, Bytef *back = s->window + (int)match, *here = s->window + start;
s->window + start, length) != EQUAL) { IPos len = length;
fprintf(stderr, " start %u, match %u, length %d\n", if (match == (IPos)-1) {
start, match, length); /* 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 { do {
fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); fprintf(stderr, "(%02x %02x)", *back++, *here++);
} while (--length != 0); } while (--len != 0);
z_error("invalid match"); z_error("invalid match");
} }
if (z_verbose > 1) { if (z_verbose > 1) {