Author: jilles
Date: Tue Nov 22 23:46:56 2011
New Revision: 227852
URL: http://svn.freebsd.org/changeset/base/227852

Log:
  fdopendir(): Do not close the passed file descriptor on failure.
  
  Reviewed by:  delphij

Modified:
  head/lib/libc/gen/opendir.c

Modified: head/lib/libc/gen/opendir.c
==============================================================================
--- head/lib/libc/gen/opendir.c Tue Nov 22 23:27:59 2011        (r227851)
+++ head/lib/libc/gen/opendir.c Tue Nov 22 23:46:56 2011        (r227852)
@@ -75,6 +75,8 @@ __opendir2(const char *name, int flags)
 {
        int fd;
        struct stat statb;
+       DIR *dir;
+       int saved_errno;
 
        /*
         * stat() before _open() because opening of special files may be
@@ -89,7 +91,13 @@ __opendir2(const char *name, int flags)
        if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY)) == -1)
                return (NULL);
 
-       return __opendir_common(fd, name, flags);
+       dir = __opendir_common(fd, name, flags);
+       if (dir == NULL) {
+               saved_errno = errno;
+               _close(fd);
+               errno = saved_errno;
+       }
+       return (dir);
 }
 
 static int
@@ -110,6 +118,7 @@ __opendir_common(int fd, const char *nam
        int incr;
        int saved_errno;
        int unionstack;
+       int fd2;
        struct stat statb;
 
        dirp = NULL;
@@ -199,14 +208,15 @@ __opendir_common(int fd, const char *nam
                 * which has also been read -- see fts.c.
                 */
                if (flags & DTF_REWIND) {
-                       (void)_close(fd);
-                       if ((fd = _open(name, O_RDONLY | O_DIRECTORY)) == -1) {
+                       if ((fd2 = _open(name, O_RDONLY | O_DIRECTORY)) == -1) {
                                saved_errno = errno;
                                free(buf);
                                free(dirp);
                                errno = saved_errno;
                                return (NULL);
                        }
+                       (void)_dup2(fd2, fd);
+                       _close(fd2);
                }
 
                /*
@@ -309,7 +319,6 @@ __opendir_common(int fd, const char *nam
 fail:
        saved_errno = errno;
        free(dirp);
-       (void)_close(fd);
        errno = saved_errno;
        return (NULL);
 }
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to