From: Ian Jackson Date: Fri, 15 Sep 2017 12:44:50 +0000 (+0100) Subject: tools/xenstore: get_handle: Allocate struct before opening fd X-Git-Tag: archive/raspbian/4.11.1-1+rpi1~1^2~66^2~1146 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=edf4af67047c0b829227205b75f63764d3478d32;p=xen.git tools/xenstore: get_handle: Allocate struct before opening fd Now we can also abolish the temporary local variable "fd" and simply use h->fd. This ordering is necessary to be able to call xentoolcore__register_active_handle sensibly. Signed-off-by: Ian Jackson Acked-by: Wei Liu --- diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c index 65cba86f93..7f85bb24d8 100644 --- a/tools/xenstore/xs.c +++ b/tools/xenstore/xs.c @@ -223,27 +223,26 @@ static struct xs_handle *get_handle(const char *connect_to) { struct stat buf; struct xs_handle *h = NULL; - int fd = -1, saved_errno; + int saved_errno; + + h = malloc(sizeof(*h)); + if (h == NULL) + goto err; + + memset(h, 0, sizeof(*h)); + h->fd = -1; if (stat(connect_to, &buf) != 0) goto err; if (S_ISSOCK(buf.st_mode)) - fd = get_socket(connect_to); + h->fd = get_socket(connect_to); else - fd = get_dev(connect_to); + h->fd = get_dev(connect_to); - if (fd == -1) + if (h->fd == -1) goto err; - h = malloc(sizeof(*h)); - if (h == NULL) - goto err; - - memset(h, 0, sizeof(*h)); - - h->fd = fd; - INIT_LIST_HEAD(&h->reply_list); INIT_LIST_HEAD(&h->watch_list); @@ -267,7 +266,10 @@ static struct xs_handle *get_handle(const char *connect_to) err: saved_errno = errno; - if (fd >= 0) close(fd); + if (h) { + if (h->fd >= 0) + close(h->fd); + } free(h); errno = saved_errno;