Skip to content

Fix gzopen/gzclose correctness issues - #66

Merged
asonje merged 2 commits into
mainfrom
fix/gzopen-gzclose-correctness
Jul 30, 2026
Merged

Fix gzopen/gzclose correctness issues#66
asonje merged 2 commits into
mainfrom
fix/gzopen-gzclose-correctness

Conversation

@mulugetam

Copy link
Copy Markdown
Contributor
  • 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.

@asonje
asonje self-requested a review July 30, 2026 19:37
mulugetam and others added 2 commits July 30, 2026 13:28
- 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
asonje merged commit 1a2dda0 into main Jul 30, 2026
3 checks passed
@asonje
asonje deleted the fix/gzopen-gzclose-correctness branch July 30, 2026 22:29
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