diff options
author | jilles <jilles@FreeBSD.org> | 2011-11-22 23:46:56 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2011-11-22 23:46:56 +0000 |
commit | e322b8dfb2862a7c69ba8b24ff8b8ff0dc50cffe (patch) | |
tree | 4e70084986c77666a35b97c7558dc149a2c8ddd2 /lib/libc | |
parent | 3946e53dadc723ebbf76020baf1067e004ed37ab (diff) | |
download | FreeBSD-src-e322b8dfb2862a7c69ba8b24ff8b8ff0dc50cffe.zip FreeBSD-src-e322b8dfb2862a7c69ba8b24ff8b8ff0dc50cffe.tar.gz |
fdopendir(): Do not close the passed file descriptor on failure.
Reviewed by: delphij
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/opendir.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 0c08735..407c047 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); } |