Patchwork [PULL,22/26] libcacard/vscclient: fix leakage of socket on error paths

login
register
mail settings
Submitter Michael Tokarev
Date June 14, 2013, 10:50 a.m.
Message ID <1371207042-17980-23-git-send-email-mjt@msgid.tls.msk.ru>
Download mbox | patch
Permalink /patch/251353/
State New
Headers show

Comments

Michael Tokarev - June 14, 2013, 10:50 a.m.
From: Alon Levy <alevy@redhat.com>

Spotted by Coverity.

Signed-off-by: Alon Levy <alevy@redhat.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 libcacard/vscclient.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Patch

diff --git a/libcacard/vscclient.c b/libcacard/vscclient.c
index ac23647..5180d29 100644
--- a/libcacard/vscclient.c
+++ b/libcacard/vscclient.c
@@ -618,18 +618,22 @@  connect_to_qemu(
     if (ret != 0) {
         /* Error */
         fprintf(stderr, "getaddrinfo failed\n");
-        return -1;
+        goto cleanup_socket;
     }
 
     if (connect(sock, server->ai_addr, server->ai_addrlen) < 0) {
         /* Error */
         fprintf(stderr, "Could not connect\n");
-        return -1;
+        goto cleanup_socket;
     }
     if (verbose) {
         printf("Connected (sizeof Header=%zd)!\n", sizeof(VSCMsgHeader));
     }
     return sock;
+
+cleanup_socket:
+    closesocket(sock);
+    return -1;
 }
 
 int
@@ -759,5 +763,6 @@  main(
     g_io_channel_unref(channel_socket);
     g_byte_array_unref(socket_to_send);
 
+    closesocket(sock);
     return 0;
 }