Skip to content

Commit f886568

Browse files
committed
client session BUUGFIX correct socket error handling
Fixes #187
1 parent 34a6d74 commit f886568

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/session_client.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ nc_sock_connect(const char *host, uint16_t port, int timeout, int *sock_pending,
13261326
i = getaddrinfo(host, port_s, &hints, &res_list);
13271327
if (i != 0) {
13281328
ERR("Unable to translate the host address (%s).", gai_strerror(i));
1329-
return -1;
1329+
goto error;
13301330
}
13311331

13321332
for (res = res_list; res != NULL; res = res->ai_next) {
@@ -1340,16 +1340,14 @@ nc_sock_connect(const char *host, uint16_t port, int timeout, int *sock_pending,
13401340
opt = 1;
13411341
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
13421342
ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
1343-
close(sock);
1344-
return -1;
1343+
goto error;
13451344
}
13461345

13471346
if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
13481347
buf = malloc(INET6_ADDRSTRLEN);
13491348
if (!buf) {
13501349
ERRMEM;
1351-
close(sock);
1352-
return -1;
1350+
goto error;
13531351
}
13541352
if (res->ai_family == AF_INET) {
13551353
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
@@ -1359,8 +1357,7 @@ nc_sock_connect(const char *host, uint16_t port, int timeout, int *sock_pending,
13591357
if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
13601358
ERR("Converting host to IP address failed (%s).", strerror(errno));
13611359
free(buf);
1362-
close(sock);
1363-
return -1;
1360+
goto error;
13641361
}
13651362

13661363
*ip_host = buf;
@@ -1376,6 +1373,15 @@ nc_sock_connect(const char *host, uint16_t port, int timeout, int *sock_pending,
13761373
}
13771374

13781375
return sock;
1376+
1377+
error:
1378+
if (sock != -1) {
1379+
close(sock);
1380+
}
1381+
if (sock_pending) {
1382+
*sock_pending = -1;
1383+
}
1384+
return -1;
13791385
}
13801386

13811387
static NC_MSG_TYPE

0 commit comments

Comments
 (0)