[gnet-dev] [PATCH] Close socket on error condition



There's a potential fd leak in the tcp connect code, and I've seen it in
practice as well, when trying to connect to invalid IPs or getting a
destination-unreachable immediately when trying to connect. There are a
couple of other error conditions that can potentially cause this too.

So here's a small patch to remedy that.

--
Hans Petter

--- gnet-2.0.3-orig/src/tcp.c	Tue May 13 20:16:46 2003
+++ gnet-2.0.3/src/tcp.c	Fri Aug  8 19:33:24 2003
@@ -409,10 +409,16 @@ gnet_tcp_socket_new_async_direct (const 
   /* Get the flags (should all be 0?) */
   flags = fcntl(sockfd, F_GETFL, 0);
   if (flags == -1)
+  {
+    GNET_CLOSE_SOCKET(sockfd);
     return NULL;
+  }
 
   if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
+  {
+    GNET_CLOSE_SOCKET(sockfd);
     return NULL;
+  }
 
   /* Create our structure */
   s = g_new0(GTcpSocket, 1);
@@ -425,6 +431,7 @@ gnet_tcp_socket_new_async_direct (const 
     {
       if (errno != EINPROGRESS)
 	{
+	  GNET_CLOSE_SOCKET(s->sockfd);
 	  g_free(s);
 	  return NULL;
 	}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]