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)
committerIan Beckwith <ianb@erislabs.net>
Thu, 9 Jun 2011 18:49:50 +0000 (19:49 +0100)
* 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.
(cherry picked from commit d94bbd1eb1fc483d72397ec5dd94f7e885e12440)

ChangeLog
lib/opendir-safer.c

index 54bf81e..aaaa436 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 f1e5fb7..3726f88 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;