Skip to content

Reject invalid level and strategy in gzsetparams(). - #1424

Open
chiliec wants to merge 2 commits into
madler:developfrom
chiliec:gzsetparams-validate
Open

chiliec wants to merge 2 commits into
madler:developfrom
chiliec:gzsetparams-validate

Conversation

@chiliec

@chiliec chiliec commented Nov 25, 2026 •

Copy link
Copy Markdown

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.

Invalid values were stored and Z_OK returned, so the first gzwrite()
then failed with a false out of memory error.

@ylfeid ylfeid left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@chiliec

chiliec commented Nov 27, 2026

Copy link
Copy Markdown
Author

@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 strm->avail_in && condition so the Z_BLOCK flush always runs. With it all 19,535 sizes switch levels. Output round-trips and gzip -t passes, it's clean under ASan/UBSan, and make test passes. When gzsetparams() comes right after a Z_FINISH flush, gz_comp() still waits for new data before starting another member, so there's no empty extra member.

@ylfeid

ylfeid commented Nov 29, 2026 •

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants