]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add comment to gzdopen() in zlib.h to use dup() when using fileno().
authorMark Adler <madler@alumni.caltech.edu>
Thu, 20 Oct 2011 16:07:58 +0000 (09:07 -0700)
committerMark Adler <madler@alumni.caltech.edu>
Thu, 20 Oct 2011 16:07:58 +0000 (09:07 -0700)
A problem surfaced in a multi-threaded application where fileno() was
used to get a file descriptor from an fopen(), which was then fed to
gzdopen().  The problem occurred when the gzclose() followed by the
fclose() tried to close the same file descriptor twice.  If fclose()
were not done, there would be a memory leak.  The only way out is to
dup() the file descriptor so that gzclose() closes the duplicated
file descriptor, and fclose() closes the original file descriptor.

zlib.h

diff --git a/zlib.h b/zlib.h
index 14a925bfb93d7f72d0cc154179f7051ac0af9cdf..d85d7c29ecea6c215df11b5641c9dc07835b6bec 100644 (file)
--- a/zlib.h
+++ b/zlib.h
@@ -1233,7 +1233,11 @@ ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
    descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
    fd.  If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
    mode);.  The duplicated descriptor should be saved to avoid a leak, since
-   gzdopen does not close fd if it fails.
+   gzdopen does not close fd if it fails.  If you are using fileno() to get the
+   file descriptor from a FILE *, then you will have to use dup() to avoid
+   double-close()ing the file descriptor.  Both gzclose() and fclose() will
+   close the associated file descriptor, so they need to have different file
+   descriptors.
 
      gzdopen returns NULL if there was insufficient memory to allocate the
    gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not