bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH] opendir-safer.c: don't clobber errno; don't close negative FD


From: Jim Meyering
Subject: [PATCH] opendir-safer.c: don't clobber errno; don't close negative FD
Date: Tue, 24 May 2011 13:49:23 +0200

Hi Eric,

coverity reported on the potential for closing a negative file descriptor.
Fixing it, I saw/fixed the errno-clobbering problem.

Any objection?

Here's the slightly more readable form of the patch,
ignoring the indentation change:

diff --git a/lib/opendir-safer.c b/lib/opendir-safer.c
index f1e5fb7..3726f88 100644
--- a/lib/opendir-safer.c
+++ b/lib/opendir-safer.c
@@ -50,10 +50,18 @@ opendir_safer (char const *name)
           int e;
 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR
           int f = dup_safer (fd);
+          if (f < 0)
+            {
+              e = errno;
+              newdp = NULL;
+            }
+          else
+            {
           newdp = fdopendir (f);
           e = errno;
           if (! newdp)
             close (f);
+            }
 #else /* !FDOPENDIR */
           newdp = opendir_safer (name);
           e = errno;


>From d94bbd1eb1fc483d72397ec5dd94f7e885e12440 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Tue, 24 May 2011 13:44:41 +0200
Subject: [PATCH] opendir-safer.c: don't clobber errno; don't close negative
 FD

* lib/opendir-safer.c (opendir_safer):
[HAVE_FDOPENDIR || GNULIB_FDOPENDIR]: Don't close a negative
file descriptor, and more importantly, don't clobber the
offending errno value with EINVAL.  Before, upon failure
of dup_safer, we would pass the negative file descriptor to
fdopendir, which would clobber errno.
---
 ChangeLog           |   10 ++++++++++
 lib/opendir-safer.c |   16 ++++++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1ab8584..b7be3f3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-05-24  Jim Meyering  <address@hidden>
+
+       opendir-safer.c: don't clobber errno; don't close negative FD
+       * lib/opendir-safer.c (opendir_safer):
+       [HAVE_FDOPENDIR || GNULIB_FDOPENDIR]: Don't close a negative
+       file descriptor, and more importantly, don't clobber the
+       offending errno value with EINVAL.  Before, upon failure
+       of dup_safer, we would pass the negative file descriptor to
+       fdopendir, which would clobber errno.
+
 2011-05-23  Bruno Haible  <address@hidden>

        idcache: Fix module description.
diff --git a/lib/opendir-safer.c b/lib/opendir-safer.c
index f1e5fb7..3726f88 100644
--- a/lib/opendir-safer.c
+++ b/lib/opendir-safer.c
@@ -50,10 +50,18 @@ opendir_safer (char const *name)
           int e;
 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR
           int f = dup_safer (fd);
-          newdp = fdopendir (f);
-          e = errno;
-          if (! newdp)
-            close (f);
+          if (f < 0)
+            {
+              e = errno;
+              newdp = NULL;
+            }
+          else
+            {
+              newdp = fdopendir (f);
+              e = errno;
+              if (! newdp)
+                close (f);
+            }
 #else /* !FDOPENDIR */
           newdp = opendir_safer (name);
           e = errno;
--
1.7.5.2.585.gfbd48



reply via email to

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