From 9d513beeca70252c7430d2f06f957d8dfab7f8ad Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Tue, 27 Aug 2019 10:42:40 +0200 Subject: [PATCH] GOption: Allow NULL arguments to parse_strv Close issue #873 --- glib/goption.c | 8 +++++--- glib/tests/option-context.c | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/glib/goption.c b/glib/goption.c index 183e9d9d3..38b12af3e 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -2708,8 +2708,10 @@ g_option_context_get_description (GOptionContext *context) /** * g_option_context_parse_strv: * @context: a #GOptionContext - * @arguments: (inout) (array zero-terminated=1): a pointer to the - * command line arguments (which must be in UTF-8 on Windows) + * @arguments: (inout) (array null-terminated=1) (optional): a pointer + * to the command line arguments (which must be in UTF-8 on Windows). + * Starting with GLib 2.62, @arguments can be %NULL, which matches + * g_option_context_parse(). * @error: a return location for errors * * Parses the command line arguments. @@ -2743,7 +2745,7 @@ g_option_context_parse_strv (GOptionContext *context, gint argc; context->strv_mode = TRUE; - argc = g_strv_length (*arguments); + argc = arguments && *arguments ? g_strv_length (*arguments) : 0; success = g_option_context_parse (context, &argc, arguments, error); context->strv_mode = FALSE; diff --git a/glib/tests/option-context.c b/glib/tests/option-context.c index 34ebfaaf6..f4ff746d3 100644 --- a/glib/tests/option-context.c +++ b/glib/tests/option-context.c @@ -1912,6 +1912,11 @@ missing_arg_test (void) g_strfreev (argv_copy); g_free (argv); g_option_context_free (context); + + /* Checking g_option_context_parse_strv on NULL args */ + context = g_option_context_new (NULL); + g_assert_true (g_option_context_parse_strv (context, NULL, NULL)); + g_option_context_free (context); } static gchar *test_arg;