connect: Close open but unconnected socket in singleipconnect()

singleipconnect() could return the file descriptor of an open socket
even though the function returned a CURLE_COULDNT_CONNECT error code
from commit ed1662c374 and 02fbc26d59.

This could cause tests 19, 704 and 1233 to fail on FreeBSD, AIX and
Solaris.
This commit is contained in:
Steve Holme
2013-11-16 22:56:11 +00:00
parent b56d7cda74
commit 2c7a5578e1

View File

@@ -562,6 +562,7 @@ static CURLcode trynextip(struct connectdata *conn,
while(ai && ai->ai_family != family) while(ai && ai->ai_family != family)
ai = ai->ai_next; ai = ai->ai_next;
if(ai) { if(ai) {
rc = singleipconnect(conn, ai, &conn->tempsock[tempindex]); rc = singleipconnect(conn, ai, &conn->tempsock[tempindex]);
conn->tempaddr[tempindex] = ai; conn->tempaddr[tempindex] = ai;
@@ -1027,10 +1028,8 @@ singleipconnect(struct connectdata *conn,
conn->bits.ipv6 = (addr.family == AF_INET6)?TRUE:FALSE; conn->bits.ipv6 = (addr.family == AF_INET6)?TRUE:FALSE;
#endif #endif
*sockp = sockfd;
if(-1 == rc) { if(-1 == rc) {
switch (error) { switch(error) {
case EINPROGRESS: case EINPROGRESS:
case EWOULDBLOCK: case EWOULDBLOCK:
#if defined(EAGAIN) #if defined(EAGAIN)
@@ -1042,7 +1041,8 @@ singleipconnect(struct connectdata *conn,
case EAGAIN: case EAGAIN:
#endif #endif
#endif #endif
return CURLE_OK; res = CURLE_OK;
break;
default: default:
/* unknown error, fallthrough and try another address! */ /* unknown error, fallthrough and try another address! */
@@ -1051,11 +1051,15 @@ singleipconnect(struct connectdata *conn,
data->state.os_errno = error; data->state.os_errno = error;
/* connect failed */ /* connect failed */
return CURLE_COULDNT_CONNECT; Curl_closesocket(conn, sockfd);
res = CURLE_COULDNT_CONNECT;
} }
} }
return CURLE_OK; if(!res)
*sockp = sockfd;
return res;
} }
/* /*