[Spice-devel] [PATCH usbredir 4/5] Be sure to close files on error.

Jeremy White jwhite at codeweavers.com
Wed Dec 9 14:16:28 PST 2015


Signed-off-by: Jeremy White <jwhite at codeweavers.com>
---
 connectkernel/connectkernel.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/connectkernel/connectkernel.c b/connectkernel/connectkernel.c
index b9ed114..f45cf62 100644
--- a/connectkernel/connectkernel.c
+++ b/connectkernel/connectkernel.c
@@ -21,9 +21,10 @@ void usage(const char *argv0)
 
 int connect_tcp(char *server, char *port)
 {
-    struct addrinfo hints, *res, *rp;
-    int rc;
-    int s;
+    struct addrinfo hints;
+    struct addrinfo *res = NULL, *rp;
+    int rc = 0;
+    int s = -1;
 
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = AF_INET;
@@ -45,7 +46,8 @@ int connect_tcp(char *server, char *port)
         if (s < 0)
         {
             perror("socket");
-            return -2;
+            s = -2;
+            goto exit;
         }
 
         if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
@@ -56,9 +58,11 @@ int connect_tcp(char *server, char *port)
     if (! rp)
     {
         fprintf(stderr, "Error: unable to connect.\n");
-        return -3;
+        close(s);
+        s = -3;
     }
 
+exit:
     freeaddrinfo(res);
 
     return s;
@@ -83,6 +87,7 @@ int connect_unix(char *fname)
     if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) != 0)
     {
         perror("connect");
+        close(s);
         return -1;
     }
 
@@ -167,6 +172,7 @@ int main(int argc, char *argv[])
         if (write(fd, buf, strlen(buf)) < 0)
         {
             fprintf(stderr, "Attach of '%s' to kernel failed\n", buf);
+            close(fd);
             exit(1);
         }
         close(fd);
-- 
2.1.4



More information about the Spice-devel mailing list