Repository navigation
Conversation
ylfeid
left a comment
There was a problem hiding this comment.
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.
| int ret; | ||
|
|
||
| /* flush previous input with previous parameters before changing */ | ||
| if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1) |
There was a problem hiding this comment.
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>
24180d4 to
bd95339
Compare
|
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. |
|
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! |
|
Thanks for the exhaustive verification, especially covering every write size. Glad the unconditional flush holds up on your probe and under the sanitizers. |
gzsetparams()returnedZ_OKfor an out-of-range level or strategy. When the stream was not initialized yet, the values were stored unchecked, and the firstgzwrite()failed with a misleadingZ_MEM_ERROR("out of memory"), losing the data. After initialization thedeflateParams()return value was ignored, so an invalid change was also reported as accepted.Validate level and strategy as
deflateParams()does and returnZ_STREAM_ERRORbefore storing anything. When the stream is initialized, return the error fromdeflateParams()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.