This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH][BZ #15701] Freopen with closed file descriptors.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Wed, 9 Oct 2013 21:21:16 +0200
- Subject: [PATCH][BZ #15701] Freopen with closed file descriptors.
- Authentication-results: sourceware.org; auth=none
Hi,
This bug complains about undefined behavior when user closes file
descriptor. This is again patch or close case.
We could check this by asking kernel if descriptor is valid.
Comments?
* libio/freopen.c: Check if file descriptors are open.
diff --git a/libio/freopen.c b/libio/freopen.c
index 6ba37bf..ca157da 100644
--- a/libio/freopen.c
+++ b/libio/freopen.c
@@ -47,6 +47,12 @@ freopen (filename, mode, fp)
return NULL;
_IO_acquire_lock (fp);
int fd = _IO_fileno (fp);
+ /* Check that file descriptor is open. */
+ if (fcntl(fd, F_GETFD) == -1)
+ {
+ return NULL;
+ }
+
const char *gfilename = (filename == NULL && fd >= 0
? fd_to_filename (fd) : filename);
fp->_flags2 |= _IO_FLAGS2_NOCLOSE;