opendir-safer.c: don't clobber errno; don't close negative FD
authorJim Meyering <meyering@redhat.com>
Tue, 24 May 2011 11:44:41 +0000 (13:44 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 24 May 2011 11:44:41 +0000 (13:44 +0200)
* 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
lib/opendir-safer.c

index 1ab8584d5ce668dbe98a468db82de756c876a441..b7be3f368e0b53829a521b54ecf672417a4e3ba0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-05-24  Jim Meyering  <meyering@redhat.com>
+
+       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  <bruno@clisp.org>
 
        idcache: Fix module description.
index f1e5fb7aa88ebdce4a8493e2eb8c911a775e4258..3726f88b13f5a74afd7141447ca7673ee711e5a7 100644 (file)
@@ -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;