This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]