Skip to content

Reject invalid parameters in gzsetparams() - #1425

Open
UOS-liang wants to merge 1 commit into
madler:developfrom
UOS-liang:gzsetparams-validate
Open

UOS-liang wants to merge 1 commit into
madler:developfrom
UOS-liang:gzsetparams-validate

Conversation

@UOS-liang

Copy link
Copy Markdown

gzsetparams() returned Z_OK for an out-of-range level or strategy. When the stream was not initialized yet, the values were stored unchecked, and the first gzwrite() failed with a misleading Z_MEM_ERROR ("out of memory"), losing the data. After initialization the deflateParams() return value was ignored, so an invalid change was also reported as accepted.

Validate level and strategy as deflateParams() does and return Z_STREAM_ERROR before storing anything. When the stream is initialized, return the error from deflateParams() instead of applying parameters that were not. Add a test that asks for level 10 and strategy 99 before the first write, then checks that writing still succeeds.

@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 #1424 , which fixes the same issue. The parameter ranges are identical, and both match deflateParams().

Checking the deflateParams() return value turns up a separate case. After a gzwrite() of at least state->size bytes, the input is compressed straight from the caller's buffer, so avail_in is 0 but deflate still holds data in its window. gzsetparams() skips gz_comp(Z_BLOCK), and deflateParams() has to flush into whatever space is left in the output buffer. When that isn't enough, it returns Z_BUF_ERROR and applies nothing.

I tested writes of 8–256 KB followed by a change from level 6 to level 0. With random data, 300 of the ~19.5k sizes hit this, and 60 did with compressible data. On develop and #1424, gzsetparams() returns Z_OK but deflate stays at level 6 for the rest of the file (state->level is already 0, so a second call is a no-op). With this PR, it returns Z_BUF_ERROR, which zlib.h doesn't list, and retrying straight away fails the same way, because nothing drains the output buffer.

Flushing unconditionally (see inline) fixed every case in my tests, the output still round-trips, and it's clean under ASan/UBSan. Happy to share the test program.

Comment thread gzwrite.c Outdated
int ret;

/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

With avail_in == 0 this flush is skipped but deflate can still hold data from a gzwrite() that compressed straight from the caller's buffer. Dropping the strm->avail_in && condition lets gz_comp() drain the output buffer and flush the window first, so deflateParams() can't hit Z_BUF_ERROR here

gzsetparams() stored level and strategy without checking them when the
stream was not initialized yet, and ignored the return value of
deflateParams() otherwise. An out-of-range level was then passed to
deflateInit2() by the first gzwrite(), which failed and was reported as
Z_MEM_ERROR "out of memory", losing the written data.

Validate both parameters as deflateParams() does, and return its error
instead of storing parameters that were not applied. Flush pending
output before the change: after a large gzwrite() the input is
compressed straight from the caller's buffer, so avail_in is 0 while
deflate still holds data in its window, and deflateParams() can fail
with Z_BUF_ERROR when the output buffer has too little room.

Add a test for both cases.

Fixes: madler#1421
Signed-off-by: Hailiang Ma <mahailiang@uniontech.com>
@UOS-liang
UOS-liang force-pushed the gzsetparams-validate branch from 24180d4 to bd95339 Compare November 30, 2026 03:08
@UOS-liang

Copy link
Copy Markdown
Author

Good catch, reproduced it: with incompressible data, gzsetparams() returned Z_BUF_ERROR for writes from 16396 bytes on, with avail_in == 0 but the deflate window still holding data.

Pushed bd95339. It drops the avail_in condition so gz_comp(Z_BLOCK) writes out the pending output first, and adds a test that changes the level after a large write and checks that the data round-trips. The branch is rebased on the current develop and passes under ASan/UBSan.

@ylfeid

ylfeid commented Dec 1, 2026

Copy link
Copy Markdown

Verified bd95339 with the same probe as on #1424: after a gzwrite() of 8–256 KB of random data, switch from level 6 to 0 and check the deflate state's level. On develop the change silently doesn't apply for 300 of 19,535 write sizes; with bd95339 it applies for all of them. Output round-trips, invalid level or strategy now return Z_STREAM_ERROR, test/example.c passes, and it's all clean under ASan/UBSan. Thanks!

@UOS-liang

Copy link
Copy Markdown
Author

Thanks for the exhaustive verification, especially covering every write size. Glad the unconditional flush holds up on your probe and under the sanitizers.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants