summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPekka Paalanen <pekka.paalanen@collabora.co.uk>2014-08-07 16:46:52 +0300
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>2014-08-07 16:59:14 +0300
commitced769ac92c96c4ef8dab585b02c9d211482b5ee (patch)
tree376781c2263eed8f30d4d46ff0020123751c1633
parentserver: fix error handling when adding socket (diff)
downloadwayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar.gz
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar.bz2
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar.lz
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar.xz
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.tar.zst
wayland-ced769ac92c96c4ef8dab585b02c9d211482b5ee.zip
server: fix conditions for fds in wl_socket_destroy
0 is also a valid fd, and needs to be closed. On error we set fd to -1. We need to also initialize fds to -1, so we do not accidentally close stdout on error. While fixing this, also remove one use-before-NULL-check. Based on the patch by Marek. Cc: Marek Chalupa <mchqwerty@gmail.com> Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-by: Jasper St. Pierre <jstpierre@mecheye.net>
-rw-r--r--src/wayland-server.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/wayland-server.c b/src/wayland-server.c
index 3c162d4..3bd8e68 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -848,16 +848,32 @@ wl_socket_destroy(struct wl_socket *s)
wl_event_source_remove(s->source);
if (s->addr.sun_path[0])
unlink(s->addr.sun_path);
- if (s->fd)
+ if (s->fd >= 0)
close(s->fd);
if (s->lock_addr[0])
unlink(s->lock_addr);
- if (s->fd_lock)
+ if (s->fd_lock >= 0)
close(s->fd_lock);
free(s);
}
+static struct wl_socket *
+wl_socket_alloc(void)
+{
+ struct wl_socket *s;
+
+ s = malloc(sizeof *s);
+ if (!s)
+ return NULL;
+
+ memset(s, 0, sizeof *s);
+ s->fd = -1;
+ s->fd_lock = -1;
+
+ return s;
+}
+
WL_EXPORT void
wl_display_destroy(struct wl_display *display)
{
@@ -1149,12 +1165,10 @@ wl_display_add_socket_auto(struct wl_display *display)
* you need more than this, use the explicit add_socket API. */
const int MAX_DISPLAYNO = 32;
- s = malloc(sizeof *s);
+ s = wl_socket_alloc();
if (s == NULL)
return NULL;
- memset(s, 0, sizeof *s);
-
do {
snprintf(display_name, sizeof display_name, "wayland-%d", displayno);
if (wl_socket_init_for_display_name(s, display_name) < 0) {
@@ -1184,8 +1198,7 @@ wl_display_add_socket(struct wl_display *display, const char *name)
{
struct wl_socket *s;
- s = malloc(sizeof *s);
- memset(s, 0, sizeof *s);
+ s = wl_socket_alloc();
if (s == NULL)
return -1;