From 2a9719213094e98e06ffe89eefca842d612bb71d Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Mon, 16 Aug 2010 14:57:45 +0000
Subject: [PATCH] bug 1362: fixed handling of return codes of
 getaddrinfo/getprotobyname_r, which return >0 on error

---
 LCS/Common/src/Net/Socket.cc | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/LCS/Common/src/Net/Socket.cc b/LCS/Common/src/Net/Socket.cc
index c4e677dc5f9..cc41e29b69c 100644
--- a/LCS/Common/src/Net/Socket.cc
+++ b/LCS/Common/src/Net/Socket.cc
@@ -396,11 +396,12 @@ int32 Socket::initTCPSocket(bool	asServer)
 		  struct addrinfo*		hostEnt;		// server host entry
 
 		  // No, try to resolve the name
-		  if (!getaddrinfo(itsHost.c_str(), NULL, &hints, &hostEnt)) {
+		  if (getaddrinfo(itsHost.c_str(), NULL, &hints, &hostEnt) != 0) {
 		    LOG_ERROR(formatString("Socket:Hostname (%s) can not be resolved",
 														itsHost.c_str()));
 		    return (itsErrno = BADHOST);
 		  }
+
 		  memcpy (&IPbytes, &reinterpret_cast<struct sockaddr_in *>(hostEnt->ai_addr)->sin_addr, sizeof IPbytes);
 
           freeaddrinfo(hostEnt);
@@ -415,28 +416,28 @@ int32 Socket::initTCPSocket(bool	asServer)
 	itsProtocolType = 6;		// assume tcp
 	if (!(itsTCPAddr.sin_port = htons((uint16)atoi(itsPort.c_str())))) {
 		LOG_ERROR(formatString("Socket:Portnr/service(%s) can not be resolved",
-													itsHost.c_str()));
+													itsPort.c_str()));
 		return (itsErrno = PORT);
 	}
 #else
 	struct addrinfo*	servEnt;		// service info entry
-	if ((getaddrinfo(NULL, itsPort.c_str(), &hints, &servEnt))) {
-		itsTCPAddr.sin_port = reinterpret_cast<struct sockaddr_in *>(servEnt->ai_addr)->sin_port;
-        freeaddrinfo(servEnt);
-	}
-	else {
+
+	if (getaddrinfo(NULL, itsPort.c_str(), &hints, &servEnt) != 0) {
 		LOG_ERROR(formatString(
 					"Socket:Portnr/service(%s) can not be resolved",
-					itsHost.c_str()));
+					itsPort.c_str()));
 		return (itsErrno = PORT);
 	}
 
+	itsTCPAddr.sin_port = reinterpret_cast<struct sockaddr_in *>(servEnt->ai_addr)->sin_port;
+    freeaddrinfo(servEnt);
+
 	// Try to map protocol name to protocol number
 	const char*			protocol = (itsType == UDP ? "udp" : "tcp");
 	struct protoent*	protoEnt;
 	struct protoent 	protoEnt_buf;
     char                buf[1024];
-	if (!getprotobyname_r(protocol, &protoEnt_buf, buf, sizeof buf, &protoEnt)) {
+	if (getprotobyname_r(protocol, &protoEnt_buf, buf, sizeof buf, &protoEnt) != 0) {
 	  return (itsErrno = PROTOCOL);
 	}
 	itsProtocolType = protoEnt->p_proto;
-- 
GitLab