From f7bf69443df17ca4b58256030c337b0369c7ab80 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 25 Oct 2018 16:43:36 +0100 Subject: [PATCH] sysdeps: Remove trailing NUL from command lines from /proc Resolves: https://gitlab.freedesktop.org/dbus/dbus/issues/222 Signed-off-by: Simon McVittie --- dbus/dbus-sysdeps-util-unix.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 24eba4e31..262dcfded 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -1092,7 +1092,22 @@ string_squash_nonprintable (DBusString *str) buf = _dbus_string_get_udata (str); len = _dbus_string_get_length (str); - + + /* /proc/$pid/cmdline is a sequence of \0-terminated words, but we + * want a sequence of space-separated words, with no extra trailing + * space: + * "/bin/sleep" "\0" "60" "\0" + * -> "/bin/sleep" "\0" "60" + * -> "/bin/sleep" " " "60" + * + * so chop off the trailing NUL before cleaning up unprintable + * characters. */ + if (len > 0 && buf[len - 1] == '\0') + { + _dbus_string_shorten (str, 1); + len--; + } + for (i = 0; i < len; i++) { unsigned char c = (unsigned char) buf[i]; -- 2.47.3