From be52e658171edf6651895c40d1563289bafa52f7 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 23 Feb 2015 18:02:29 -0800 Subject: Fix dup2 in the case where the two fds are equal. dup3's behavior differs from dup2 in this case, so we need to paper over that in the C library. Change-Id: I313cd6f226db5e237f61866f324c5ecdd12bf762 --- libc/bionic/dup2.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'libc/bionic/dup2.cpp') diff --git a/libc/bionic/dup2.cpp b/libc/bionic/dup2.cpp index 0b8632bee..98c56469d 100644 --- a/libc/bionic/dup2.cpp +++ b/libc/bionic/dup2.cpp @@ -26,8 +26,19 @@ * SUCH DAMAGE. */ +#include #include 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); } -- cgit v1.2.3