Fix gzopen/gzclose correctness issues - #66
Merged
Conversation
asonje
self-requested a review
July 30, 2026 19:37
- gzopen: check for open() failure (fd < 0) and gzdopen() failure (file == NULL) before registering the file in the map. Previously a failed open would pass fd=-1 to gzdopen, store a NULL-keyed map entry, and leak the fd on gzdopen failure. - gzclose: use off_t instead of int for the lseek return value to avoid truncation on files larger than 2 GiB. The truncated value caused truncate() to corrupt large files. Signed-off-by: Mulugeta Mammo <mulugeta.mammo@intel.com>
gzclose() called gzip_files.Unset(file) after orig_gzclose(file) had already freed the gzFile, on all three exit paths. The map is keyed by the gzFile address, so unsetting after the free can erase the entry of whatever file was allocated at that address in the meantime. Move the Unset to just after the lookup and drop the two trailing calls. The shared_ptr from ShardedMap::Get() keeps this file's state alive for the rest of the function. Defensive rather than a fix for an observed failure: under glibc a freed chunk returns to the freeing thread, so another thread's gzopen does not receive it. Allocators with cross-thread free lists offer no such guarantee, and unsetting once up front beats getting the ordering right on three exit paths. Signed-off-by: Olasoji <olasoji.denloye@intel.com>
asonje
approved these changes
Jul 30, 2026
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.
gzopen: check for open() failure (fd < 0) and gzdopen() failure (file == NULL) before registering the file in the map. Previously a failed open would pass fd=-1 to gzdopen, store a NULL-keyed map entry, and leak the fd on gzdopen failure.
gzclose: use off_t instead of int for the lseek return value to avoid truncation on files larger than 2 GiB. The truncated value caused truncate() to corrupt large files.