Skip to content

Commit

Permalink
Fix to 2e672f3 decompress_peek_gz change.
Browse files Browse the repository at this point in the history
The "zs.total_out < destsize" should have been "zs.avail_out" to be
more robust to total_out being reset by inflateReset.  However looking
again neither the avail_in or avail_out checks are necessary, as once
we hit the end of either input or output buffer the next cycle
triggers ret == Z_BUF_ERROR and we drop out as normal.

Thanks to John Marshall for the spot.
  • Loading branch information
jkbonfield committed Jul 13, 2023
1 parent 2e672f3 commit b0fc3a1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ decompress_peek_gz(hFILE *fp, unsigned char *dest, size_t destsize)
unsigned char *last_in = buffer;
while (zs.total_out < destsize) {
ret = inflate(&zs, Z_SYNC_FLUSH);
if (ret == Z_STREAM_END && zs.avail_in && zs.total_out < destsize) {
if (ret == Z_STREAM_END) {
if (last_in == zs.next_in)
break; // paranoia to avoid potential looping
break; // Paranoia to avoid potential looping. Shouldn't happen
else
last_in = zs.next_in;
inflateReset(&zs);
continue;
}
if (ret != Z_OK)
} else if (ret != Z_OK) {
// eg Z_BUF_ERROR due to avail_in/out becoming zero
break;
}
}

// zs.total_out can sometimes be wrong as inflateReset resets it
// NB: zs.total_out is changed by inflateReset, so use pointer diff instead
destsize = zs.next_out - dest;
inflateEnd(&zs);

Expand Down

0 comments on commit b0fc3a1

Please sign in to comment.