]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus-daemon: Avoid known options being interpreted as optional arguments
authorXin Shi <shixin21@huawei.com>
Fri, 28 Jul 2023 01:38:24 +0000 (09:38 +0800)
committerSimon McVittie <smcv@collabora.com>
Tue, 1 Aug 2023 18:25:07 +0000 (18:25 +0000)
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 <shixin21@huawei.com>
bus/main.c

index c1ae5e726b1ff315bcca9973e8b12992693dbf03..cf1abc64c6417ddba3dac081211323a081c8c19d 100644 (file)
@@ -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 ();