From aa9de5b1c0ed9cd7ae0dec0dcaf19c18be846d7e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 1 Nov 2021 09:01:40 +0900 Subject: process-util: handle double NUL as the end of command line Fixes #21186. --- src/basic/process-util.c | 6 ++++++ src/test/test-process-util.c | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 54648462ad..1b96d3ca85 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -214,11 +214,17 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags assert(!(flags & PROCESS_CMDLINE_USE_LOCALE)); _cleanup_strv_free_ char **args = NULL; + char **p; args = strv_parse_nulstr(t, k); if (!args) return -ENOMEM; + /* Drop trailing empty strings. See issue #21186. */ + STRV_FOREACH_BACKWARDS(p, args) + if (isempty(*p)) + *p = mfree(*p); + ans = quote_command_line(args, shflags); if (!ans) return -ENOMEM; diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index a180f3f9b4..ab093a7457 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -484,8 +484,8 @@ TEST(get_process_cmdline_harder) { /* Test with multiple arguments that do require quoting */ #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0" -#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\"" -#define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``' \"\"" +#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\"" +#define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``'" assert_se(lseek(fd, SEEK_SET, 0) == 0); assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1); assert_se(ftruncate(fd, sizeof CMDLINE1) == 0); @@ -503,8 +503,8 @@ TEST(get_process_cmdline_harder) { line = mfree(line); #define CMDLINE2 "foo\0\1\2\3\0\0" -#define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\"" -#define EXPECT2p "foo $'\\001\\002\\003' \"\" \"\"" +#define EXPECT2 "foo \"\\001\\002\\003\"" +#define EXPECT2p "foo $'\\001\\002\\003'" assert_se(lseek(fd, SEEK_SET, 0) == 0); assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2); assert_se(ftruncate(fd, sizeof CMDLINE2) == 0); -- cgit v1.2.3