swrap: Treat the case of fd == newfd correctly in dup2()
authorMichael Adam <obnox@samba.org>
Tue, 16 Aug 2016 08:59:40 +0000 (10:59 +0200)
committerAndreas Schneider <asn@samba.org>
Tue, 23 Aug 2016 14:27:48 +0000 (16:27 +0200)
Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>

Signed-off-by: Michael Adam <obnox@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
src/socket_wrapper.c

index 00518c1f567331ea40b9e850ffcc4bdac220c579..0b13a8c47802471c618c287d570118bbe6aba1d1 100644 (file)
@@ -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 */