zlib-ng: deflateBound() returns a value that is too small

The following (IMHO reasonable) test fails:

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>

int main(void) {
  unsigned char Plain[1] = "\x06";
  unsigned char Compressed[130];
  z_stream Strm;
  int Bound;

  memset(&Strm, 0, sizeof(Strm));
  assert(deflateInit2(&Strm, 0, 8, 31, 1, Z_DEFAULT_STRATEGY) == Z_OK);
  Bound = deflateBound(&Strm, 1);
  Strm.next_in = Plain;
  Strm.next_out = Compressed;
  Strm.avail_in = sizeof(Plain);
  Strm.avail_out = sizeof(Compressed);
  assert(deflate(&Strm, Z_FINISH) == Z_STREAM_END);
  assert(Strm.avail_in == 0);
  assert(Bound >= Strm.next_out - Compressed);
  assert(deflateEnd(&Strm) == Z_OK);
}

The actual compressed data size is 24, the deflateBound() return value is 23. Tested on x86_64, commit 81d91d6f.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Both zlib and zlib-ng emit an empty stored block on level 0, which is at least 5 bytes. On level 1 we get a fixed block instead.