[PATCH][BZ #15701] Freopen with closed file descriptors.

Ondřej Bílka neleai@seznam.cz
Wed Oct 9 19:21:00 GMT 2013


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;



More information about the Libc-alpha mailing list