From e13b7872e047b15c0cf3092f1317ed019d17c990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 19 Mar 2026 18:13:41 +0100 Subject: [PATCH] options: only consume "--" immediately after an option that stops parsing 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 | 6 ++++-- src/test/test-options.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/shared/options.c b/src/shared/options.c index 09b677f813..903ed62440 100644 --- a/src/shared/options.c +++ b/src/shared/options.c @@ -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; diff --git a/src/test/test-options.c b/src/test/test-options.c index 6ca29fb849..ab3a429369 100644 --- a/src/test/test-options.c +++ b/src/test/test-options.c @@ -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")); }