From 34ee852317a3592e70b88089acf0c8328c145495 Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Tue, 2 May 2006 12:20:39 +0000 Subject: [PATCH] fixed leak in short option parsing. rewrote parts of the code to be more Tue May 2 14:18:25 2006 Tim Janik * glib/goption.c (g_option_context_parse): fixed leak in short option parsing. rewrote parts of the code to be more concise to enhance readability. fixed exaggerated uses of strlen. --- ChangeLog | 6 +++++ ChangeLog.pre-2-12 | 6 +++++ glib/goption.c | 56 ++++++++++++++-------------------------------- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2b551cc11..b70598e58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue May 2 14:18:25 2006 Tim Janik + + * glib/goption.c (g_option_context_parse): fixed leak in short + option parsing. rewrote parts of the code to be more concise to + enhance readability. fixed exaggerated uses of strlen. + 2006-04-28 Behdad Esfahbod * glib/guniprop.c: #include diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 2b551cc11..b70598e58 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,9 @@ +Tue May 2 14:18:25 2006 Tim Janik + + * glib/goption.c (g_option_context_parse): fixed leak in short + option parsing. rewrote parts of the code to be more concise to + enhance readability. fixed exaggerated uses of strlen. + 2006-04-28 Behdad Esfahbod * glib/guniprop.c: #include diff --git a/glib/goption.c b/glib/goption.c index 446b6e3b6..a5026ec2a 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -1446,36 +1446,23 @@ g_option_context_parse (GOptionContext *context, continue; } else - { - /* short option */ - - gint new_i, j; + { /* short option */ + gint j, new_i = i, arg_length; gboolean *nulled_out = NULL; - arg = (*argv)[i] + 1; - - new_i = i; - - if (context->ignore_unknown) - nulled_out = g_new0 (gboolean, strlen (arg)); - - for (j = 0; j < strlen (arg); j++) + arg_length = strlen (arg); + nulled_out = g_newa (gboolean, arg_length); + memset (nulled_out, 0, arg_length * sizeof (gboolean)); + for (j = 0; j < arg_length; j++) { if (context->help_enabled && arg[j] == '?') print_help (context, TRUE, NULL); - parsed = FALSE; - if (context->main_group && !parse_short_option (context, context->main_group, i, &new_i, arg[j], argc, argv, error, &parsed)) - { - - g_free (nulled_out); - goto fail; - } - + goto fail; if (!parsed) { /* Try the groups */ @@ -1483,47 +1470,38 @@ g_option_context_parse (GOptionContext *context, while (list) { GOptionGroup *group = list->data; - if (!parse_short_option (context, group, i, &new_i, arg[j], argc, argv, error, &parsed)) goto fail; - if (parsed) break; - list = list->next; } } - - if (context->ignore_unknown) - { - if (parsed) - nulled_out[j] = TRUE; - else - continue; - } - - if (!parsed) - break; + + if (context->ignore_unknown && parsed) + nulled_out[j] = TRUE; + else if (context->ignore_unknown) + continue; + else if (!parsed) + break; + /* !context->ignore_unknown && parsed */ } - if (context->ignore_unknown) { gchar *new_arg = NULL; gint arg_index = 0; - - for (j = 0; j < strlen (arg); j++) + for (j = 0; j < arg_length; j++) { if (!nulled_out[j]) { if (!new_arg) - new_arg = g_malloc (strlen (arg) + 1); + new_arg = g_malloc (arg_length + 1); new_arg[arg_index++] = arg[j]; } } if (new_arg) new_arg[arg_index] = '\0'; - add_pending_null (context, &((*argv)[i]), new_arg); } else if (parsed)