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

[Xen-devel] [PATCH 13/22] tools/xenstore: get_handle: use "goto err" error handling style



Replace the ad-hoc exit clauses with the error handling style where
  - local variables contain either things to be freed, or sentinels
  - all error exits go via an "err" label which frees everything

Signed-off-by: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
---
 tools/xenstore/xs.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c
index 56caac7..65cba86 100644
--- a/tools/xenstore/xs.c
+++ b/tools/xenstore/xs.c
@@ -226,7 +226,7 @@ static struct xs_handle *get_handle(const char *connect_to)
        int fd = -1, saved_errno;
 
        if (stat(connect_to, &buf) != 0)
-               return NULL;
+               goto err;
 
        if (S_ISSOCK(buf.st_mode))
                fd = get_socket(connect_to);
@@ -234,15 +234,11 @@ static struct xs_handle *get_handle(const char 
*connect_to)
                fd = get_dev(connect_to);
 
        if (fd == -1)
-               return NULL;
+               goto err;
 
        h = malloc(sizeof(*h));
-       if (h == NULL) {
-               saved_errno = errno;
-               close(fd);
-               errno = saved_errno;
-               return NULL;
-       }
+       if (h == NULL)
+               goto err;
 
        memset(h, 0, sizeof(*h));
 
@@ -267,6 +263,15 @@ static struct xs_handle *get_handle(const char *connect_to)
 #endif
 
        return h;
+
+err:
+       saved_errno = errno;
+
+       if (fd >= 0) close(fd);
+       free(h);
+
+       errno = saved_errno;
+       return NULL;
 }
 
 struct xs_handle *xs_daemon_open(void)
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.