diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-10-20 09:07:58 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-10-20 09:07:58 -0700 |
commit | 6dbf1d10263dbf2f49d12d788f89ab308ba4ca6d (patch) | |
tree | 64e85e4c0341acf45b7d78c08edaee64161fc0a5 /zlib.h | |
parent | 5ab9f47745fe9353291b217f705086b6070575d5 (diff) | |
download | zlib-6dbf1d10263dbf2f49d12d788f89ab308ba4ca6d.tar.gz zlib-6dbf1d10263dbf2f49d12d788f89ab308ba4ca6d.tar.bz2 zlib-6dbf1d10263dbf2f49d12d788f89ab308ba4ca6d.zip |
Add comment to gzdopen() in zlib.h to use dup() when using fileno().
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.
Diffstat (limited to 'zlib.h')
-rw-r--r-- | zlib.h | 6 |
1 files changed, 5 insertions, 1 deletions
@@ -1233,7 +1233,11 @@ ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); | |||
1233 | descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor | 1233 | descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor |
1234 | fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, | 1234 | fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, |
1235 | mode);. The duplicated descriptor should be saved to avoid a leak, since | 1235 | mode);. The duplicated descriptor should be saved to avoid a leak, since |
1236 | gzdopen does not close fd if it fails. | 1236 | gzdopen does not close fd if it fails. If you are using fileno() to get the |
1237 | file descriptor from a FILE *, then you will have to use dup() to avoid | ||
1238 | double-close()ing the file descriptor. Both gzclose() and fclose() will | ||
1239 | close the associated file descriptor, so they need to have different file | ||
1240 | descriptors. | ||
1237 | 1241 | ||
1238 | gzdopen returns NULL if there was insufficient memory to allocate the | 1242 | gzdopen returns NULL if there was insufficient memory to allocate the |
1239 | gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not | 1243 | gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not |