client session BUUGFIX correct socket error handling
Fixes #187
diff --git a/src/session_client.c b/src/session_client.c
index 83abb92..b0e115c 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -1347,7 +1347,7 @@
i = getaddrinfo(host, port_s, &hints, &res_list);
if (i != 0) {
ERR("Unable to translate the host address (%s).", gai_strerror(i));
- return -1;
+ goto error;
}
for (res = res_list; res != NULL; res = res->ai_next) {
@@ -1361,16 +1361,14 @@
opt = 1;
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
- close(sock);
- return -1;
+ goto error;
}
if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
buf = malloc(INET6_ADDRSTRLEN);
if (!buf) {
ERRMEM;
- close(sock);
- return -1;
+ goto error;
}
if (res->ai_family == AF_INET) {
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
@@ -1380,8 +1378,7 @@
if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
ERR("Converting host to IP address failed (%s).", strerror(errno));
free(buf);
- close(sock);
- return -1;
+ goto error;
}
*ip_host = buf;
@@ -1397,6 +1394,15 @@
}
return sock;
+
+error:
+ if (sock != -1) {
+ close(sock);
+ }
+ if (sock_pending) {
+ *sock_pending = -1;
+ }
+ return -1;
}
static NC_MSG_TYPE