From 04dacf2d2e764eb9fd0791642893b4e1bc199ef0 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Tue, 16 Aug 2016 10:59:40 +0200 Subject: swrap: Treat the case of fd == newfd correctly in dup2() Pair-Programmed-With: Stefan Metzmacher Signed-off-by: Michael Adam Signed-off-by: Stefan Metzmacher Reviewed-by: Andreas Schneider --- src/socket_wrapper.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index 00518c1..0b13a8c 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -5186,6 +5186,16 @@ static int swrap_dup2(int fd, int newfd) return libc_dup2(fd, newfd); } + if (fd == newfd) { + /* + * According to the manpage: + * + * "If oldfd is a valid file descriptor, and newfd has the same + * value as oldfd, then dup2() does nothing, and returns newfd." + */ + return newfd; + } + if (find_socket_info(newfd)) { /* dup2() does an implicit close of newfd, which we * need to emulate */ -- cgit