[client] make close() on socket handle more intuitive (resolves 'double close' bugs flagged by Coverity)

This commit is contained in:
Michael Rash
2014-07-22 22:04:44 -04:00
parent 73490209f7
commit 666d150aff
2 changed files with 13 additions and 15 deletions
+8 -7
View File
@@ -54,7 +54,7 @@ struct url
static int
try_url(struct url *url, fko_cli_options_t *options)
{
int sock=-1, res, error, http_buf_len, i;
int sock=-1, sock_success=0, res, error, http_buf_len, i;
int bytes_read = 0, position = 0;
int o1, o2, o3, o4;
struct addrinfo *result, *rp, hints;
@@ -108,19 +108,20 @@ try_url(struct url *url, fko_cli_options_t *options)
continue;
if ((error = (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)))
{
sock_success = 1;
break; /* made it */
}
}
if (! sock_success)
{
log_msg(LOG_VERBOSITY_ERROR, "resolve_ip_http: Could not create socket: ", strerror(errno));
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
if (rp == NULL)
{
log_msg(LOG_VERBOSITY_ERROR, "resolve_ip_http: Could not create socket: ", strerror(errno));
close(sock);
return(-1);
}
+5 -8
View File
@@ -86,7 +86,7 @@ static int
send_spa_packet_tcp_or_udp(const char *spa_data, const int sd_len,
const fko_cli_options_t *options)
{
int sock, res=0, error;
int sock=-1, sock_success=0, res=0, error;
struct addrinfo *result, *rp, hints;
char port_str[MAX_PORT_STR_LEN+1] = {0};
@@ -134,16 +134,13 @@ send_spa_packet_tcp_or_udp(const char *spa_data, const int sd_len,
continue;
if ((error = (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)))
{
sock_success = 1;
break; /* made it */
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
}
if (rp == NULL) {
if (! sock_success) {
log_msg(LOG_VERBOSITY_ERROR,
"send_spa_packet_tcp_or_udp: Could not create socket: ",
strerror(errno));