[MPlayer-cvslog] r36656 - trunk/stream/tcp.c

reimar subversion at mplayerhq.hu
Sun Jan 19 23:24:53 CET 2014


Author: reimar
Date: Sun Jan 19 23:24:53 2014
New Revision: 36656

Log:
tcp: fix socket/descriptor leak on error.

Modified:
   trunk/stream/tcp.c

Modified: trunk/stream/tcp.c
==============================================================================
--- trunk/stream/tcp.c	Sun Jan 19 23:24:52 2014	(r36655)
+++ trunk/stream/tcp.c	Sun Jan 19 23:24:53 2014	(r36656)
@@ -77,7 +77,8 @@ static const char *af2String(int af) {
 
 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 @@ connect2Server_with_af(char *host, int p
 	// 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 @@ connect2Server_with_af(char *host, int p
 
 	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 @@ connect2Server_with_af(char *host, int p
 #endif
 		default:
 			mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
-			return TCP_ERROR_FATAL;
+			goto err_out;
 	}
 
 
@@ -159,10 +160,10 @@ connect2Server_with_af(char *host, int p
 #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 @@ connect2Server_with_af(char *host, int p
 #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 @@ connect2Server_with_af(char *host, int p
 		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 @@ connect2Server_with_af(char *host, int p
 		  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 @@ connect2Server_with_af(char *host, int p
 	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


More information about the MPlayer-cvslog mailing list