Skip to content

Commit

Permalink
Fix bug in deflateBound() for level 0 and memLevel 9.
Browse files Browse the repository at this point in the history
memLevel 9 would cause deflateBound() to assume the use of fixed
blocks, even if the compression level was 0, which forces stored
blocks. That could result in a bound less than the size of the
compressed data. Now level 0 always uses the stored blocks bound.
  • Loading branch information
madler authored and Dead2 committed Feb 3, 2023
1 parent 33e671b commit 4af454b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,15 @@ unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long

/* if not default parameters, return conservative bound */
if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) || /* hook for IBM Z DFLTCC */
s->w_bits != 15 || HASH_BITS < 15)
s->w_bits != 15 || HASH_BITS < 15) {
if (s->level == 0) {
/* upper bound for stored blocks with length 127 (memLevel == 1) --
~4% overhead plus a small constant */
complen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + (sourceLen >> 11) + 7;
}

return complen + wraplen;
}

#ifndef NO_QUICK_STRATEGY
return sourceLen /* The source size itself */
Expand Down

0 comments on commit 4af454b

Please sign in to comment.