]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
options: only consume "--" immediately after an option that stops parsing
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Thu, 19 Mar 2026 17:13:41 +0000 (18:13 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Sun, 22 Mar 2026 15:52:22 +0000 (16:52 +0100)
The behaviour that was implemented in systemd-dissect was that
both '--exec -- cmd' and '--exec cmd' result in 'cmd' as the command,
and '--' anywhere later is as a positional argument, so nesting is
possible, e.g.:
  --exec -- cmd --opt -- another-cmd --another-opt

This is not obvious, so add some tests for this and keep it as a separate
commit.

src/shared/options.c
src/test/test-options.c

index 09b677f8133120b3aedcbc685667a60ef9cef8ac..903ed62440fd1a44f0fd22b338eee70ffa42f48b 100644 (file)
@@ -112,8 +112,10 @@ int option_parse(
                                 return 0;
                         }
 
-                        if (!state->parsing_stopped &&
-                            argv[state->optind][0] == '-' &&
+                        if (state->parsing_stopped)
+                                return 0;
+
+                        if (argv[state->optind][0] == '-' &&
                             argv[state->optind][1] != '\0')
                                 /* Looks like we found an option parameter */
                                 break;
index 6ca29fb849ab907e4d1ecde5f9fad557acfcc136..ab3a42936958cd36a7306ac2e8145ac2e1567e20 100644 (file)
@@ -977,6 +977,21 @@ TEST(option_macros) {
                               NULL);
 
         /* OPTION_STOPS_PARSING then "--": "--" is still consumed after exec */
+        test_macros_parse_one(STRV_MAKE("arg0",
+                                        "--help",
+                                        "--exec",
+                                        "--",
+                                        "--version",
+                                        "-h"),
+                              (Entry[]) {
+                                      { "help" },
+                                      { "exec" },
+                                      {}
+                              },
+                              STRV_MAKE("--version",
+                                        "-h"));
+
+        /* OPTION_STOPS_PARSING then later "--": "--" is not consumed */
         test_macros_parse_one(STRV_MAKE("arg0",
                                         "--help",
                                         "--exec",
@@ -989,6 +1004,24 @@ TEST(option_macros) {
                                       {}
                               },
                               STRV_MAKE("--version",
+                                        "--",
+                                        "-h"));
+
+        /* OPTION_STOPS_PARSING then "--" twice: second "--" is not consumed */
+        test_macros_parse_one(STRV_MAKE("arg0",
+                                        "--help",
+                                        "--exec",
+                                        "--",
+                                        "--",
+                                        "--version",
+                                        "-h"),
+                              (Entry[]) {
+                                      { "help" },
+                                      { "exec" },
+                                      {}
+                              },
+                              STRV_MAKE("--",
+                                        "--version",
                                         "-h"));
 }