From: Simon McVittie Date: Thu, 25 Oct 2018 15:43:36 +0000 (+0100) Subject: sysdeps: Remove trailing NUL from command lines from /proc X-Git-Tag: dbus-1.13.8~38^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7bf69443df17ca4b58256030c337b0369c7ab80;p=thirdparty%2Fdbus.git sysdeps: Remove trailing NUL from command lines from /proc Resolves: https://gitlab.freedesktop.org/dbus/dbus/issues/222 Signed-off-by: Simon McVittie --- 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];