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@eu.citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
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);
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));
#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)