From 08a35d307fff30efc89564552948b0192eff6e56 Mon Sep 17 00:00:00 2001 From: Daniel Stone Date: Wed, 16 Nov 2016 16:37:05 +0000 Subject: [PATCH] xwayland: Fix X11 lock file size confusion The X11 lock file was somewhat opaque. Into a sized array of 16 characters, we previously read 11 bytes. 61beda653b fixed the parsing of this input to ensure that we only considered the first 10 bytes: this has the effect of culling a LF byte at the end of the string. This commit more explicitly NULLs the entire string before reading, and trims trailing LF characters only. It also adds some documentation by way of resizing pid, an explicit size check on snprintf's return, and comments. Verified manually that it emits lock files with a trailing \n, as Xorg does. Also verified manually that it ignores misformatted lock files, but accepts either \n or \0 in the trailing position. Related Mutter issue: https://bugzilla.gnome.org/show_bug.cgi?id=774613 Signed-off-by: Daniel Stone Reviewed-by: Pekka Paalanen Reviewed-by: Eric Engestrom --- xwayland/launcher.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/xwayland/launcher.c b/xwayland/launcher.c index 97d7c6ee..276795af 100644 --- a/xwayland/launcher.c +++ b/xwayland/launcher.c @@ -148,7 +148,9 @@ bind_to_unix_socket(int display) static int create_lockfile(int display, char *lockfile, size_t lsize) { - char pid[16]; + /* 10 decimal characters, trailing LF and NUL byte; see comment + * at end of function. */ + char pid[11]; int fd, size; pid_t other; @@ -166,7 +168,7 @@ create_lockfile(int display, char *lockfile, size_t lsize) return -1; } - /* Trim the newline, ensure terminated string. */ + /* Trim the trailing LF, or at least ensure it's NULL. */ pid[10] = '\0'; if (!safe_strtoint(pid, &other)) { @@ -199,10 +201,12 @@ create_lockfile(int display, char *lockfile, size_t lsize) return -1; } - /* Subtle detail: we use the pid of the wayland - * compositor, not the xserver in the lock file. */ - size = snprintf(pid, sizeof pid, "%10d\n", getpid()); - if (write(fd, pid, size) != size) { + /* Subtle detail: we use the pid of the wayland compositor, not the + * xserver in the lock file. + * Also subtle is that we don't emit a trailing NUL to the file, so + * our size here is 11 rather than 12. */ + size = dprintf(fd, "%10d\n", getpid()); + if (size != 11) { unlink(lockfile); close(fd); return -1;