changeset 14810:fdcc880c0998

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.
author Jim Meyering <meyering@redhat.com>
date Tue, 24 May 2011 13:44:41 +0200
parents e271eb6448af
children 64be6c8e9bab
files ChangeLog lib/opendir-safer.c
diffstat 2 files changed, 22 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/opendir-safer.c
+++ b/lib/opendir-safer.c
@@ -50,10 +50,18 @@
           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;