<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On 17 July 2014 19:54, Jasper St. Pierre <span dir="ltr"><<a href="mailto:jstpierre@mecheye.net" target="_blank">jstpierre@mecheye.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">We'll use this to autodetect a good socket to open on.<br>
---<br>
 src/wayland-server.c | 41 ++++++++++++++++++++++++++---------------<br>
 1 file changed, 26 insertions(+), 15 deletions(-)<br>
<br>
diff --git a/src/wayland-server.c b/src/wayland-server.c<br>
index d919eb2..3350751 100644<br>
--- a/src/wayland-server.c<br>
+++ b/src/wayland-server.c<br>
@@ -1060,11 +1060,9 @@ get_socket_lock(struct wl_socket *socket)<br>
        return fd_lock;<br>
 }<br>
<br>
-WL_EXPORT int<br>
-wl_display_add_socket(struct wl_display *display, const char *name)<br>
+static int<br>
+wl_socket_init_for_display_name(struct wl_socket *s, const char *name)<br>
 {<br>
-       struct wl_socket *s;<br>
-       socklen_t size;<br>
        int name_size;<br>
        const char *runtime_dir;<br>
<br>
@@ -1078,15 +1076,6 @@ wl_display_add_socket(struct wl_display *display, const char *name)<br>
                return -1;<br>
        }<br>
<br>
-       s = malloc(sizeof *s);<br>
-       if (s == NULL)<br>
-               return -1;<br>
-<br>
-       if (name == NULL)<br>
-               name = getenv("WAYLAND_DISPLAY");<br>
-       if (name == NULL)<br>
-               name = "wayland-0";<br>
-<br>
        memset(&s->addr, 0, sizeof s->addr);<br>
        s->addr.sun_family = AF_LOCAL;<br>
        name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,<br>
@@ -1096,13 +1085,35 @@ wl_display_add_socket(struct wl_display *display, const char *name)<br>
        if (name_size > (int)sizeof s->addr.sun_path) {<br>
                wl_log("error: socket path \"%s/%s\" plus null terminator"<br>
                       " exceeds 108 bytes\n", runtime_dir, name);<br>
-               wl_socket_destroy(s);<br>
                /* to prevent programs reporting<br>
                 * "failed to add socket: Success" */<br>
                errno = ENAMETOOLONG;<br>
                return -1;<br>
        };<br>
<br>
+       return 0;<br>
+}<br>
+<br>
+WL_EXPORT int<br>
+wl_display_add_socket(struct wl_display *display, const char *name)<br>
+{<br>
+       struct wl_socket *s;<br>
+       socklen_t size;<br>
+<br>
+       s = malloc(sizeof *s);<br>
+       if (s == NULL)<br>
+               return -1;<br>
+<br>
+       if (name == NULL)<br>
+               name = getenv("WAYLAND_DISPLAY");<br>
+       if (name == NULL)<br>
+               name = "wayland-0";<br>
+<br>
+       if (wl_socket_init_for_display_name(s, name) < 0) {<br>
+               wl_socket_destroy(s);<br>
+               return -1;<br>
+       }<br>
+<br>
        s->fd_lock = get_socket_lock(s);<br>
        if (s->fd_lock < 0) {<br>
                wl_socket_destroy(s);<br>
@@ -1115,7 +1126,7 @@ wl_display_add_socket(struct wl_display *display, const char *name)<br>
                return -1;<br>
        }<br>
<br>
-       size = offsetof (struct sockaddr_un, sun_path) + name_size;<br>
+       size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);<br></blockquote><div><br>name_size in the original code is counting with the terminating zero, strlen returns size of the string<br>excluding terminating zero, so here you are off by one.<br>
<br></div><div>Maybe we could return name_size from the wl_socket_init_for_display_name, it's returning int anyway<br>and we would reuse already counted value. However, I don't mind strlen.<br></div><div><br></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
        if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {<br>
                wl_log("bind() failed with error: %m\n");<br>
                wl_socket_destroy(s);<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.0.1<br>
<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>
</font></span></blockquote></div><br></div><div class="gmail_extra">Regards,<br></div><div class="gmail_extra">Marek<br></div></div>