[PATCH] Fix option parser: treat current option as argument of the prvious option

Chengwei Yang chengwei.yang at intel.com
Sun Apr 7 21:49:44 PDT 2013


There is a bug in option parser if the previous option need an argument.
In that case, the current option will be treated as argument of the
previous option regardless what in fact it is.

So the issue looks like below:
$ dbus-daemon --session --print-address --print-pid
Invalid file descriptor: "--print-pid"

This commit stricts the condition by check if the current option is
supposed to be an option (prefix with '-').

Signed-off-by: Chengwei Yang <chengwei.yang at intel.com>
---
 bus/main.c |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/bus/main.c b/bus/main.c
index ca0be3c..065a12f 100644
--- a/bus/main.c
+++ b/bus/main.c
@@ -341,6 +341,15 @@ close_reload_pipe (DBusWatch **watch)
 }
 #endif /* DBUS_UNIX */
 
+static int
+is_option (const char *arg)
+{
+    if (arg && arg[0] == '-')
+        return 1;
+
+    return 0;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -438,7 +447,7 @@ main (int argc, char **argv)
           if (!_dbus_string_append (&config_file, file))
             exit (1);
         }
-      else if (prev_arg &&
+      else if (prev_arg && !is_option(arg) &&
                strcmp (prev_arg, "--config-file") == 0)
         {
           check_two_config_files (&config_file, "config-file");
@@ -462,7 +471,7 @@ main (int argc, char **argv)
           if (!_dbus_string_append (&address, file))
             exit (1);
         }
-      else if (prev_arg &&
+      else if (prev_arg && !is_option(arg) &&
                strcmp (prev_arg, "--address") == 0)
         {
           check_two_addresses (&address, "address");
@@ -488,7 +497,7 @@ main (int argc, char **argv)
 
           print_address = TRUE;
         }
-      else if (prev_arg &&
+      else if (prev_arg && !is_option(arg) &&
                strcmp (prev_arg, "--print-address") == 0)
         {
           check_two_addr_descriptors (&addr_fd, "print-address");
@@ -516,7 +525,7 @@ main (int argc, char **argv)
 
           print_pid = TRUE;
         }
-      else if (prev_arg &&
+      else if (prev_arg && !is_option(arg) &&
                strcmp (prev_arg, "--print-pid") == 0)
         {
           check_two_pid_descriptors (&pid_fd, "print-pid");
-- 
1.7.9.5



More information about the dbus mailing list