From 2108309302ec66c71646b762ee32d8ef0ad9618e Mon Sep 17 00:00:00 2001 From: Samuel Cabrero Date: Wed, 29 Jun 2022 05:42:52 +0200 Subject: [PATCH] unix,tcp: fix errno handling in uv__tcp_bind() (#3652) The errno value is only meaningful if bind() fails and returns -1. Some bind() implementations may return success but modify errno value internally, like the socket_wrapper library used by the Samba testsuite. --- src/unix/tcp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/unix/tcp.c b/src/unix/tcp.c index 789807f0..f1040e98 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -184,14 +184,15 @@ int uv__tcp_bind(uv_tcp_t* tcp, #endif errno = 0; - if (bind(tcp->io_watcher.fd, addr, addrlen) && errno != EADDRINUSE) { + err = bind(tcp->io_watcher.fd, addr, addrlen); + if (err == -1 && errno != EADDRINUSE) { if (errno == EAFNOSUPPORT) /* OSX, other BSDs and SunoS fail with EAFNOSUPPORT when binding a * socket created with AF_INET to an AF_INET6 address or vice versa. */ return UV_EINVAL; return UV__ERR(errno); } - tcp->delayed_error = UV__ERR(errno); + tcp->delayed_error = (err == -1) ? UV__ERR(errno) : 0; tcp->flags |= UV_HANDLE_BOUND; if (addr->sa_family == AF_INET6)