summaryrefslogtreecommitdiffstats
path: root/libc/bionic
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-02-24 05:55:37 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-02-24 05:55:37 +0000
commit2aef607b25c463baed5ae70d14212e24ea7bcf2b (patch)
tree0f58f6022d63e08166da1a03775d50df4d2f13b5 /libc/bionic
parent393bdb156d5024a03f03425a977d0518c84dbb98 (diff)
parentbe52e658171edf6651895c40d1563289bafa52f7 (diff)
downloadbionic-2aef607b25c463baed5ae70d14212e24ea7bcf2b.zip
bionic-2aef607b25c463baed5ae70d14212e24ea7bcf2b.tar.gz
bionic-2aef607b25c463baed5ae70d14212e24ea7bcf2b.tar.bz2
Merge "Fix dup2 in the case where the two fds are equal."
Diffstat (limited to 'libc/bionic')
-rw-r--r--libc/bionic/dup2.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/libc/bionic/dup2.cpp b/libc/bionic/dup2.cpp
index 0b8632b..98c5646 100644
--- a/libc/bionic/dup2.cpp
+++ b/libc/bionic/dup2.cpp
@@ -26,8 +26,19 @@
* SUCH DAMAGE.
*/
+#include <fcntl.h>
#include <unistd.h>
int dup2(int old_fd, int new_fd) {
+ // If old_fd is equal to new_fd and a valid file descriptor, dup2 returns
+ // old_fd without closing it. This is not true of dup3, so we have to
+ // handle this case ourselves.
+ if (old_fd == new_fd) {
+ if (fcntl(old_fd, F_GETFD) == -1) {
+ return -1;
+ }
+ return old_fd;
+ }
+
return dup3(old_fd, new_fd, 0);
}