bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] dirent-safer: fix cloexec race


From: Paul Eggert
Subject: [PATCH 2/2] dirent-safer: fix cloexec race
Date: Sat, 12 Aug 2017 11:36:25 -0700

* lib/opendir-safer.c: Include fcntl.h instead of unistd-safer.h.
(opendir_safer): Use F_DUPFD_CLOEXEC.
* modules/dirent-safer (Depends-on): Add fcntl.  Remove unistd-safer.
* tests/test-dirent-safer.c: Do not include unistd-safer.h,
as it is no longer a prerequisite.  Use F_DUPFD_CLOEXEC
instead of dup_safer.
---
 ChangeLog                 | 8 ++++++++
 lib/opendir-safer.c       | 4 ++--
 modules/dirent-safer      | 2 +-
 tests/test-dirent-safer.c | 4 +---
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c725f5d91..514b503e5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2017-08-12  Paul Eggert  <address@hidden>
 
+       dirent-safer: fix cloexec race
+       * lib/opendir-safer.c: Include fcntl.h instead of unistd-safer.h.
+       (opendir_safer): Use F_DUPFD_CLOEXEC.
+       * modules/dirent-safer (Depends-on): Add fcntl.  Remove unistd-safer.
+       * tests/test-dirent-safer.c: Do not include unistd-safer.h,
+       as it is no longer a prerequisite.  Use F_DUPFD_CLOEXEC
+       instead of dup_safer.
+
        fts: fix cloexec races
        * lib/fts.c [!_LIBC]: Do not include dirent--.h, unistd--.h, cloexec.h.
        (opendirat, diropen): Use O_CLOEXEC instead of set_cloexec_flag.
diff --git a/lib/opendir-safer.c b/lib/opendir-safer.c
index b05ff549c..5f1f49e32 100644
--- a/lib/opendir-safer.c
+++ b/lib/opendir-safer.c
@@ -22,8 +22,8 @@
 #include "dirent-safer.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
-#include "unistd-safer.h"
 
 /* Like opendir, but do not clobber stdin, stdout, or stderr.  */
 
@@ -49,7 +49,7 @@ opendir_safer (char const *name)
           DIR *newdp;
           int e;
 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR
-          int f = dup_safer (fd);
+          int f = fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
           if (f < 0)
             {
               e = errno;
diff --git a/modules/dirent-safer b/modules/dirent-safer
index b38c58e64..4ea4a2561 100644
--- a/modules/dirent-safer
+++ b/modules/dirent-safer
@@ -11,8 +11,8 @@ Depends-on:
 dirent
 closedir
 dirfd
+fcntl
 opendir
-unistd-safer
 
 configure.ac:
 gl_DIRENT_SAFER
diff --git a/tests/test-dirent-safer.c b/tests/test-dirent-safer.c
index 2f8c3cde2..8c4e4e358 100644
--- a/tests/test-dirent-safer.c
+++ b/tests/test-dirent-safer.c
@@ -25,8 +25,6 @@
 #include <stdio.h>
 #include <unistd.h>
 
-#include "unistd-safer.h"
-
 /* This test intentionally closes stderr.  So, we arrange to have fd 10
    (outside the range of interesting fd's during the test) set up to
    duplicate the original stderr.  */
@@ -75,7 +73,7 @@ main (void)
 
 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
       {
-        int fd = dup_safer (dfd);
+        int fd = fcntl (dfd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
         ASSERT (STDERR_FILENO < fd);
         dp = fdopendir (fd);
         ASSERT (dp);
-- 
2.13.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]