From 86a89967f2ff8daf210d8abd0da2542e325b1ba3 Mon Sep 17 00:00:00 2001 From: Xin Shi Date: Fri, 28 Jul 2023 09:38:24 +0800 Subject: [PATCH] dbus-daemon: Avoid known options being interpreted as optional arguments The man page and --help imply that dbus-daemon --print-address --print-pid is a valid/useful thing to do, but because --print-address takes an optional argument, it is ambiguous whether --print-pid is meant to be the argument for --print-address (same as --print-address=--print-pid) or a new option (same as --print-address=1 --print-pid). In fact, before this commit, the dbus-daemon would interpret --print-pid as the optional argument to --print-address, and then fail to parse it because it isn't an integer. Because none of our options are syntactically valid as arguments for any option that takes an optional argument, we can avoid the ambiguity by delaying parsing of optional arguments until all known options have been tried. Resolves: dbus/dbus#467 Signed-off-by: Xin Shi --- bus/main.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/bus/main.c b/bus/main.c index c1ae5e72..cf1abc64 100644 --- a/bus/main.c +++ b/bus/main.c @@ -584,16 +584,6 @@ main (int argc, char **argv) if (!_dbus_string_append (&addr_fd, desc)) exit (1); - print_address = TRUE; - } - else if (prev_arg && - strcmp (prev_arg, "--print-address") == 0) - { - check_two_addr_descriptors (&addr_fd, "print-address"); - - if (!_dbus_string_append (&addr_fd, arg)) - exit (1); - print_address = TRUE; } else if (strcmp (arg, "--print-address") == 0) @@ -612,16 +602,6 @@ main (int argc, char **argv) if (!_dbus_string_append (&pid_fd, desc)) exit (1); - print_pid = TRUE; - } - else if (prev_arg && - strcmp (prev_arg, "--print-pid") == 0) - { - check_two_pid_descriptors (&pid_fd, "print-pid"); - - if (!_dbus_string_append (&pid_fd, arg)) - exit (1); - print_pid = TRUE; } else if (strcmp (arg, "--print-pid") == 0) @@ -642,6 +622,26 @@ main (int argc, char **argv) } } #endif + else if (prev_arg && + strcmp (prev_arg, "--print-address") == 0) + { + check_two_addr_descriptors (&addr_fd, "print-address"); + + if (!_dbus_string_append (&addr_fd, arg)) + exit (1); + + print_address = TRUE; + } + else if (prev_arg && + strcmp (prev_arg, "--print-pid") == 0) + { + check_two_pid_descriptors (&pid_fd, "print-pid"); + + if (!_dbus_string_append (&pid_fd, arg)) + exit (1); + + print_pid = TRUE; + } else { usage ();