# HG changeset patch # User reimar # Date 1390170293 0 # Node ID 67e2b2a5ce427b1a3687d7944cc6dac709559f66 # Parent 85b5f38299fb223dd939cfc2c768ad12cda42c75 tcp: fix socket/descriptor leak on error. diff -r 85b5f38299fb -r 67e2b2a5ce42 stream/tcp.c --- a/stream/tcp.c Sun Jan 19 22:24:52 2014 +0000 +++ b/stream/tcp.c Sun Jan 19 22:24:53 2014 +0000 @@ -77,7 +77,8 @@ static int connect2Server_with_af(char *host, int port, int af,int verb) { - int socket_server_fd; + int err_res = TCP_ERROR_FATAL; + int socket_server_fd = -1; int err; socklen_t err_len; int ret,count = 0; @@ -105,7 +106,7 @@ // our winsock name resolution code can not handle IPv6 if (af == AF_INET6) { mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n"); - return TCP_ERROR_FATAL; + goto err_out; } #endif @@ -114,7 +115,7 @@ if( socket_server_fd==-1 ) { // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af)); - return TCP_ERROR_FATAL; + goto err_out; } #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO) @@ -136,7 +137,7 @@ #endif default: mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); - return TCP_ERROR_FATAL; + goto err_out; } @@ -159,10 +160,10 @@ #endif if( hp==NULL ) { if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host); - return TCP_ERROR_FATAL; + goto err_out; } - if (af != hp->h_addrtype) return TCP_ERROR_FATAL; + if (af != hp->h_addrtype) goto err_out; memcpy( our_s_addr, hp->h_addr_list[0], hp->h_length ); } @@ -188,7 +189,7 @@ #endif default: mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); - return TCP_ERROR_FATAL; + goto err_out; } #if HAVE_INET_PTON @@ -212,8 +213,8 @@ if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) { #endif if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af)); - closesocket(socket_server_fd); - return TCP_ERROR_PORT; + err_res = TCP_ERROR_PORT; + goto err_out; } } tv.tv_sec = 0; @@ -227,7 +228,8 @@ mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout); else mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n"); - return TCP_ERROR_TIMEOUT; + err_res = TCP_ERROR_TIMEOUT; + goto err_out; } count++; FD_ZERO( &set ); @@ -249,14 +251,19 @@ ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len); if(ret < 0) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno)); - return TCP_ERROR_FATAL; + goto err_out; } if(err > 0) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err)); - return TCP_ERROR_PORT; + err_res = TCP_ERROR_PORT; + goto err_out; } return socket_server_fd; + +err_out: + if (socket_server_fd >= 0) closesocket(socket_server_fd); + return err_res; } // Connect to a server using a TCP connection