[glib: 1/2] goption: Reject group options specified with three dashes



commit 0b09890bff76a7b41e284942379184ffb57edf7d
Author: Nathan Miller <Nathan Miller wdc com>
Date:   Wed Jun 13 10:29:42 2018 -0500

    goption: Reject group options specified with three dashes
    
    Commandline option parsing would recognize group options specified
    with three dashes (i.e. ---foo 42).  This behaviour was limited to group
    options.

 glib/goption.c              |  2 +-
 glib/tests/option-context.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
---
diff --git a/glib/goption.c b/glib/goption.c
index dc9ec3bc9..d68ab5fe2 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -2028,7 +2028,7 @@ g_option_context_parse (GOptionContext   *context,
 
                   /* Now look for --<group>-<option> */
                   dash = strchr (arg, '-');
-                  if (dash)
+                  if (dash && arg < dash)
                     {
                       /* Try the groups */
                       list = context->groups;
diff --git a/glib/tests/option-context.c b/glib/tests/option-context.c
index a1e7b051c..60d3bd313 100644
--- a/glib/tests/option-context.c
+++ b/glib/tests/option-context.c
@@ -1822,6 +1822,49 @@ lonely_dash_test (void)
   g_option_context_free (context);
 }
 
+/* test that three dashes are treated as non-options */
+static void
+triple_dash_test (void)
+{
+  GOptionContext *context;
+  GOptionGroup *group;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  gchar **argv_copy;
+  int argc;
+  gint arg1, arg2;
+  GOptionEntry entries [] =
+    { { "foo", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL},
+      { NULL }
+    };
+  GOptionEntry group_entries [] =
+    { { "test", 0, 0, G_OPTION_ARG_INT, &arg2, NULL, NULL},
+      { NULL }
+    };
+
+  context = g_option_context_new (NULL);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  group = g_option_group_new ("group", "Group description", "Group help", NULL, NULL);
+  g_option_group_add_entries (group, group_entries);
+
+  g_option_context_add_group (context, group);
+
+  /* Now try parsing */
+  argv = split_string ("program ---test 42", &argc);
+  argv_copy = copy_stringv (argv, argc);
+
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION);
+  g_assert (retval == FALSE);
+
+  g_option_context_free (context);
+  g_clear_error (&error);
+  g_strfreev (argv_copy);
+  g_free (argv);
+}
+
 static void
 missing_arg_test (void)
 {
@@ -2619,6 +2662,7 @@ main (int   argc,
   /* regression tests for individual bugs */
   g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
   g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
+  g_test_add_func ("/option/bug/triple-dash", triple_dash_test);
   g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
   g_test_add_func ("/option/bug/dash-arg", dash_arg_test);
   g_test_add_func ("/option/bug/short-remaining", short_remaining);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]