<div dir="ltr"><div><div><div>It depends on implementation. For x86 bind is provided by syscall (<a href="http://lxr.free-electrons.com/source/net/socket.c#L1521">http://lxr.free-electrons.com/source/net/socket.c#L1521</a>) and for example for hurd on mach it's implemented in glibc (<a href="https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mach/hurd/bind.c;h=a42b78ac07c7a72041f07650860567b6e9547a30;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f#l33">https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mach/hurd/bind.c;h=a42b78ac07c7a72041f07650860567b6e9547a30;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f#l33</a>).<br>
<br></div>The off-by-one probably won't break anything here, but I don't see any reason why to use wrong value.<br><br></div>Cheers,<br></div>Marek<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On 18 July 2014 14:29, 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">
<div dir="ltr">Does the off-by-one matter? I don't think bind actually cares.<br></div><div class="gmail_extra"><div><div class="h5"><br><br><div class="gmail_quote">On Fri, Jul 18, 2014 at 4:13 AM, Marek Chalupa <span dir="ltr"><<a href="mailto:mchqwerty@gmail.com" target="_blank">mchqwerty@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>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></div><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"><div>
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>
</div><span><font color="#888888">--<br>
2.0.1<br>
<br>
_______________________________________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org" target="_blank">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>
</blockquote></div><br><br clear="all"><br></div></div><span class="HOEnZb"><font color="#888888">-- <br> Jasper<br>
</font></span></div>
</blockquote></div><br></div>