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.
This commit is contained in:
Nathan Miller 2018-06-13 10:29:42 -05:00
parent 2fe4fa6cd0
commit 0b09890bff
2 changed files with 45 additions and 1 deletions

View File

@ -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;

View File

@ -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);