Conversation
…dles gz_reset() left state->past uninitialised in write mode. gz_open() allocates gz_state with malloc() rather than calloc(), so a write-mode handle read the field out of indeterminate heap. Since a4e4521 both gzseek64() and gztell64() dereference past in either mode, so the uninitialised byte silently controlled whether two consecutive SEEK_CUR seeks folded their pending skip into the next offset. When the byte came up non-zero, the first seek s zero-fill was lost and the produced gzip stream was well-formed with a valid CRC and the wrong contents; gztell() likewise returned the wrong position after a deferred seek. Move the past = 0 assignment out of the GZ_READ branch so both read and write handles start with it zeroed. No struct or ABI change. Fixes: madler#1303 Reported-by: jxravi (github.com/jxravi) Signed-off-by: Brandon Barrante <aetherai@aethersystems.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1303.
gz_reset()initialisesstate->pastonly inside thestate->mode == GZ_READbranch, butgz_open()allocatesgz_statewith plainmalloc()and does not zero it. Since a4e4521,gzseek64()andgztell64()readstate->pastin either mode, so a write-mode handle readspastout of indeterminate heap.In write mode nothing ever writes to
past, so the correct value there is always 0 and the foldoffset += past ? 0 : skipingzseek64()should always happen. When the indeterminate byte comes up non-zero the fold is skipped: twoSEEK_CURseeks with no write between them drop the first seek's pending zero-fill, and the resulting gzip stream is well-formed with a valid CRC and the wrong contents.gztell()returns the wrong position after a deferred seek for the same reason, breaking thegztell(file) == gzseek(file, 0L, SEEK_CUR)equivalence documented inzlib.h.The fix is one line moved: lift
state->past = 0out of theGZ_READbranch so both modes start with it zeroed.Verified
masterate3dc0a8,-O0 -g -fsanitize=address,undefinedbuild on Debian.gzopen("wb")→ twogzseek(5, SEEK_CUR)calls →gzwrite("HELLO", 5)→ close → decompress) that expects 15 bytes reproduces the bug five runs out of five underMALLOC_PERTURB_=170on the unpatched tree (10 bytes back: five zeros dropped) and passes five out of five with the fix in place (15 bytes back: ten zeros +HELLO).make testprints *** zlib test OK *** and *** zlib 64-bit test OK ***.gzeof()all still behave the same.Notes
Reported and root-caused by @jxravi.
I used an AI coding assistant while working on this. Every hunk was reviewed by a human before commit, the reproducer and regressions were run on real hardware, and the diff is one line moved with no logic change beyond the reported bug.
Predator CI review 3-model (deepseek-chat CLEAN, glm-4.6 2 issues both FP-verified against gz_open() source, deepseek-r1 CLEAN). No confirmed defects.