diff options
author | 2003-02-05 06:19:09 +0000 | |
---|---|---|
committer | 2003-02-05 06:19:09 +0000 | |
commit | d8b7956ded4e8ee5574fc7d2d7a2d7fa6afdd4a6 (patch) | |
tree | b698994a9d6e28fd153f0b48124f0d619b22f70e /lib/libpthread/uthread | |
parent | add stdfiles test for proper fd handling (diff) | |
download | wireguard-openbsd-d8b7956ded4e8ee5574fc7d2d7a2d7fa6afdd4a6.tar.xz wireguard-openbsd-d8b7956ded4e8ee5574fc7d2d7a2d7fa6afdd4a6.zip |
handle the case where from_fd == to_fd in _thread_fd_table_dup
Diffstat (limited to 'lib/libpthread/uthread')
-rw-r--r-- | lib/libpthread/uthread/uthread_fd.c | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/lib/libpthread/uthread/uthread_fd.c b/lib/libpthread/uthread/uthread_fd.c index 4517112459f..fc7a7d353ff 100644 --- a/lib/libpthread/uthread/uthread_fd.c +++ b/lib/libpthread/uthread/uthread_fd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_fd.c,v 1.18 2003/02/05 05:51:51 marc Exp $ */ +/* $OpenBSD: uthread_fd.c,v 1.19 2003/02/05 06:19:09 marc Exp $ */ /* * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> * All rights reserved. @@ -228,25 +228,28 @@ _thread_fd_table_dup(int from_fd, int to_fd) struct fd_table_entry *entry; int ret; - /* release any existing to_fd table entry */ - entry = _thread_fd_table[to_fd]; - if (entry != NULL) { - ret = _FD_LOCK(to_fd, FD_RDWR, NULL); + if (from_fd != too_fd) { + /* release any existing to_fd table entry */ + entry = _thread_fd_table[to_fd]; + if (entry != NULL) { + ret = _FD_LOCK(to_fd, FD_RDWR, NULL); + if (ret != -1) { + if (--entry->refcnt == 0) + free(entry); + } + } else + ret = 0; + + /* to_fd is a copy of from_fd */ if (ret != -1) { - if (--entry->refcnt == 0) - free(entry); + _SPINLOCK(&fd_table_lock); + _thread_fd_table[to_fd] = _thread_fd_table[from_fd]; + _thread_fd_table[to_fd]->refcnt += 1; + _SPINUNLOCK(&fd_table_lock); } } else ret = 0; - /* to_fd is a copy of from_fd */ - if (ret != -1) { - _SPINLOCK(&fd_table_lock); - _thread_fd_table[to_fd] = _thread_fd_table[from_fd]; - _thread_fd_table[to_fd]->refcnt += 1; - _SPINUNLOCK(&fd_table_lock); - } - return (ret); } |