From e322b8dfb2862a7c69ba8b24ff8b8ff0dc50cffe Mon Sep 17 00:00:00 2001 From: jilles Date: Tue, 22 Nov 2011 23:46:56 +0000 Subject: [PATCH] fdopendir(): Do not close the passed file descriptor on failure. Reviewed by: delphij --- lib/libc/gen/opendir.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 0c08735daca9..407c0478c06b 100644 --- a/lib/libc/gen/opendir.c +++ b/lib/libc/gen/opendir.c @@ -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 *name, int flags) int incr; int saved_errno; int unionstack; + int fd2; struct stat statb; dirp = NULL; @@ -199,14 +208,15 @@ __opendir_common(int fd, const char *name, int flags) * 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 *name, int flags) fail: saved_errno = errno; free(dirp); - (void)_close(fd); errno = saved_errno; return (NULL); }