<div dir="ltr">Reviewed-by: Jasper St. Pierre <<a href="mailto:jstpierre@mecheye.net">jstpierre@mecheye.net</a>><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 7, 2014 at 9:51 AM, Pekka Paalanen <span dir="ltr"><<a href="mailto:ppaalanen@gmail.com" target="_blank">ppaalanen@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Pekka Paalanen <<a href="mailto:pekka.paalanen@collabora.co.uk">pekka.paalanen@collabora.co.uk</a>><br>
<br>
0 is also a valid fd, and needs to be closed.<br>
<br>
On error we set fd to -1. We need to also initialize fds to -1, so we do<br>
not accidentally close stdout on error.<br>
<br>
While fixing this, also remove one use-before-NULL-check.<br>
<br>
Based on the patch by Marek.<br>
<br>
Cc: Marek Chalupa <<a href="mailto:mchqwerty@gmail.com">mchqwerty@gmail.com</a>><br>
Signed-off-by: Pekka Paalanen <<a href="mailto:pekka.paalanen@collabora.co.uk">pekka.paalanen@collabora.co.uk</a>><br>
---<br>
 src/wayland-server.c | 27 ++++++++++++++++++++-------<br>
 1 file changed, 20 insertions(+), 7 deletions(-)<br>
<br>
diff --git a/src/wayland-server.c b/src/wayland-server.c<br>
index 3c162d4..3bd8e68 100644<br>
--- a/src/wayland-server.c<br>
+++ b/src/wayland-server.c<br>
@@ -848,16 +848,32 @@ wl_socket_destroy(struct wl_socket *s)<br>
<div class="">                wl_event_source_remove(s->source);<br>
        if (s->addr.sun_path[0])<br>
                unlink(s->addr.sun_path);<br>
-       if (s->fd)<br>
</div>+       if (s->fd >= 0)<br>
<div class="">                close(s->fd);<br>
        if (s->lock_addr[0])<br>
                unlink(s->lock_addr);<br>
-       if (s->fd_lock)<br>
</div>+       if (s->fd_lock >= 0)<br>
                close(s->fd_lock);<br>
<br>
        free(s);<br>
 }<br>
<br>
+static struct wl_socket *<br>
+wl_socket_alloc(void)<br>
+{<br>
+       struct wl_socket *s;<br>
+<br>
+       s = malloc(sizeof *s);<br>
+       if (!s)<br>
+               return NULL;<br>
+<br>
+       memset(s, 0, sizeof *s);<br>
+       s->fd = -1;<br>
+       s->fd_lock = -1;<br>
+<br>
+       return s;<br>
+}<br>
+<br>
 WL_EXPORT void<br>
 wl_display_destroy(struct wl_display *display)<br>
 {<br>
@@ -1149,12 +1165,10 @@ wl_display_add_socket_auto(struct wl_display *display)<br>
         * you need more than this, use the explicit add_socket API. */<br>
        const int MAX_DISPLAYNO = 32;<br>
<br>
-       s = malloc(sizeof *s);<br>
+       s = wl_socket_alloc();<br>
        if (s == NULL)<br>
                return NULL;<br>
<br>
-       memset(s, 0, sizeof *s);<br>
-<br>
        do {<br>
                snprintf(display_name, sizeof display_name, "wayland-%d", displayno);<br>
                if (wl_socket_init_for_display_name(s, display_name) < 0) {<br>
@@ -1184,8 +1198,7 @@ wl_display_add_socket(struct wl_display *display, const char *name)<br>
 {<br>
        struct wl_socket *s;<br>
<br>
-       s = malloc(sizeof *s);<br>
-       memset(s, 0, sizeof *s);<br>
+       s = wl_socket_alloc();<br>
        if (s == NULL)<br>
                return -1;<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
1.8.5.5<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>  Jasper<br>
</div>