From: Pekka Paalanen <pekka.paala...@collabora.co.uk>

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 <mchqwe...@gmail.com>
Signed-off-by: Pekka Paalanen <pekka.paala...@collabora.co.uk>
---
 src/wayland-server.c | 27 ++++++++++++++++++++-------
 1 file 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;
 
-- 
1.8.5.5

_______________________________________________
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to