diff options
author | dt <dt@FreeBSD.org> | 1998-09-13 12:48:47 +0000 |
---|---|---|
committer | dt <dt@FreeBSD.org> | 1998-09-13 12:48:47 +0000 |
commit | c5403b91f2e2a82afb9709a4d6fd26dbce22e1f7 (patch) | |
tree | 94c7254337795e9dcca2536856c702c594611deb /lib | |
parent | 3e147c2c7037039e19834af89f725d320f6eb4fe (diff) | |
download | FreeBSD-src-c5403b91f2e2a82afb9709a4d6fd26dbce22e1f7.zip FreeBSD-src-c5403b91f2e2a82afb9709a4d6fd26dbce22e1f7.tar.gz |
Don't lock newfd if it is not opened.
PR: 5961
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc_r/uthread/uthread_dup2.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/libc_r/uthread/uthread_dup2.c b/lib/libc_r/uthread/uthread_dup2.c index e9edddd..3e4ed27 100644 --- a/lib/libc_r/uthread/uthread_dup2.c +++ b/lib/libc_r/uthread/uthread_dup2.c @@ -30,6 +30,7 @@ * SUCH DAMAGE. * */ +#include <errno.h> #include <unistd.h> #ifdef _THREAD_SAFE #include <pthread.h> @@ -39,11 +40,20 @@ int dup2(int fd, int newfd) { int ret; + int newfd_opened; + + /* Check if the file descriptor is out of range: */ + if (newfd < 0 || newfd >= _thread_dtablesize) { + /* Return a bad file descriptor error: */ + errno = EBADF; + ret = -1; + } /* Lock the file descriptor: */ - if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { + else if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { /* Lock the file descriptor: */ - if ((ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) { + if (!(newfd_opened = (_thread_fd_table[newfd] != NULL)) || + (ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) { /* Perform the 'dup2' syscall: */ if ((ret = _thread_sys_dup2(fd, newfd)) < 0) { } @@ -63,7 +73,8 @@ dup2(int fd, int newfd) } /* Unlock the file descriptor: */ - _FD_UNLOCK(newfd, FD_RDWR); + if (newfd_opened) + _FD_UNLOCK(newfd, FD_RDWR); } /* Unlock the file descriptor: */ _FD_UNLOCK(fd, FD_RDWR); |