]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: handle double NUL as the end of command line 21196/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 1 Nov 2021 00:01:40 +0000 (09:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 6 Dec 2021 14:42:25 +0000 (23:42 +0900)
Fixes #21186.

src/basic/process-util.c
src/test/test-process-util.c

index 54648462ad16cdba7a069c95bf9265a0b665da2a..1b96d3ca8543079a5413a3c36872e6f56d6eabd3 100644 (file)
@@ -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;
index a180f3f9b4dfe5872ea3784494a07d3497eb28ab..ab093a7457017e47c3c5a61e8b881b300bbff869 100644 (file)
@@ -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);