From 5a126667a3e375df227be69689a150fec0d75a28 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 30 Jul 2015 12:24:36 +0200 Subject: [PATCH] Properly report connect error codes The logic was flawed and would treat all connect errors as if there were no addresses found. --- common/network/TcpSocket.cxx | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index 1ebaeecb..684ff8b3 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -145,9 +145,7 @@ TcpSocket::TcpSocket(int sock, bool close) TcpSocket::TcpSocket(const char *host, int port) : closeFd(true) { - int sock, err, result, family; - vnc_sockaddr_t sa; - socklen_t salen; + int sock, err, result; struct addrinfo *ai, *current, hints; // - Create a socket @@ -165,11 +163,13 @@ TcpSocket::TcpSocket(const char *host, int port) gai_strerror(result)); } - // This logic is too complex for the compiler to determine if - // sock is properly assigned or not. sock = -1; - + err = 0; for (current = ai; current != NULL; current = current->ai_next) { + int family; + vnc_sockaddr_t sa; + socklen_t salen; + family = current->ai_family; switch (family) { @@ -208,6 +208,7 @@ TcpSocket::TcpSocket(const char *host, int port) continue; #endif closesocket(sock); + sock = -1; break; } @@ -217,11 +218,12 @@ TcpSocket::TcpSocket(const char *host, int port) freeaddrinfo(ai); - if (current == NULL) - throw Exception("No useful address for host"); - - if (result == -1) - throw SocketException("unable connect to socket", err); + if (sock == -1) { + if (err == 0) + throw Exception("No useful address for host"); + else + throw SocketException("unable connect to socket", err); + } #ifndef WIN32 // - By default, close the socket on exec() -- 2.39.5