Repository navigation
Conversation
Invalid values were stored and Z_OK returned, so the first gzwrite() then failed with a false out of memory error.
ylfeid
left a comment
There was a problem hiding this comment.
I compared this with #1425. Same ranges, and doing the check before gz_error(state, Z_OK, NULL) is nicer, because a rejected call doesn't clear an earlier recoverable error.
One related case neither PR covers: after a gzwrite() of at least state->size bytes, gzsetparams() skips its gz_comp(Z_BLOCK) (because avail_in == 0), and deflateParams() can return Z_BUF_ERROR without applying the change. The return value is ignored here, so gzsetparams() returns Z_OK while deflate stays at the old level for the rest of the file. In my tests (8–256 KB writes, level 6 → 0), that happened for 300 of ~19.5k sizes with random data. Dropping the strm->avail_in && condition in that if fixed all of them. Details on #1425
After a gzwrite() of at least the buffer size, input is compressed straight from the user buffer, so avail_in is zero while deflate still holds data. Skipping the Z_BLOCK flush let deflateParams() return Z_BUF_ERROR without applying the new level or strategy, and gzsetparams() still returned Z_OK.
|
@ylfeid thanks, I reproduced it. After a gzwrite() of 8-256 KB of random data, switching from level 6 to 0 left deflate at level 6 for 302 of 19,535 sizes, and gzsetparams() still returned Z_OK. Pushed 55a8545, which drops the |
|
Verified 55a8545 with the same probes: all 19,535 write sizes switch levels, output round-trips, clean under ASan/UBSan, and test/example.c passes, thanks for picking it up. |
gzsetparams() stored an out-of-range level or strategy and returned Z_OK. Before the first write this made gzwrite() fail with a false "out of memory" error and lose the data; after a write the deflateParams() error was ignored. It now returns Z_STREAM_ERROR for those values, using the same ranges as deflateParams(), and zlib.h documents it.
It also always flushes deflate before deflateParams(). Before, after a large gzwrite() the flush was skipped, so the new level could silently not apply (found by @ylfeid in review).
Fixes #1421
Tested with make test (static, shared and 64-bit pass) plus a new check in test_gzio(), which fails without the change, and the repro from the issue.