From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:60633) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RYJ0Y-000193-EE for qemu-devel@nongnu.org; Wed, 07 Dec 2011 10:02:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RYJ0S-0006sO-Et for qemu-devel@nongnu.org; Wed, 07 Dec 2011 10:02:06 -0500 Received: from e06smtp10.uk.ibm.com ([195.75.94.106]:44465) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RYJ0S-0006s7-7J for qemu-devel@nongnu.org; Wed, 07 Dec 2011 10:02:00 -0500 Received: from /spool/local by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 7 Dec 2011 15:01:58 -0000 Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pB7F1u5n2363454 for ; Wed, 7 Dec 2011 15:01:56 GMT Received: from d06av01.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pB7F1tKg014581 for ; Wed, 7 Dec 2011 08:01:56 -0700 From: Stefan Hajnoczi Date: Wed, 7 Dec 2011 15:01:49 +0000 Message-Id: <1323270109-24265-3-git-send-email-stefanha@linux.vnet.ibm.com> In-Reply-To: <1323270109-24265-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1323270109-24265-1-git-send-email-stefanha@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 2/2] net: take ownership of fd in socket init functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi Today net/socket.c has no consistent policy for closing the socket file descriptor when initialization fails. This means we leak the file descriptor in some cases or we could also try to close it twice. Make error paths consistent by taking ownership of the file descriptor and closing it on error. Signed-off-by: Stefan Hajnoczi --- net/socket.c | 17 +++++++++-------- 1 files changed, 9 insertions(+), 8 deletions(-) diff --git a/net/socket.c b/net/socket.c index 613a7ef..f999c26 100644 --- a/net/socket.c +++ b/net/socket.c @@ -266,14 +266,13 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, if (saddr.sin_addr.s_addr == 0) { fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " "cannot setup multicast dst addr\n", fd); - return NULL; + goto err; } /* clone dgram socket */ newfd = net_socket_mcast_create(&saddr, NULL); if (newfd < 0) { /* error already reported by net_socket_mcast_create() */ - close(fd); - return NULL; + goto err; } /* clone newfd to fd, close newfd */ dup2(newfd, fd); @@ -283,7 +282,7 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", fd, strerror(errno)); - return NULL; + goto err; } } @@ -304,6 +303,10 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, if (is_connected) s->dgram_dst=saddr; return s; + +err: + closesocket(fd); + return NULL; } static void net_socket_connect(void *opaque) @@ -353,6 +356,7 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, (socklen_t *)&optlen)< 0) { fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); + closesocket(fd); return NULL; } switch(so_type) { @@ -386,9 +390,7 @@ static void net_socket_accept(void *opaque) } } s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); - if (!s1) { - closesocket(fd); - } else { + if (s1) { snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), "socket: connection from %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); @@ -549,7 +551,6 @@ int net_init_socket(QemuOpts *opts, } if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { - close(fd); return -1; } } else if (qemu_opt_get(opts, "listen")) { -- 1.7.7.3