fixed leak in short option parsing. rewrote parts of the code to be more
authorTim Janik <timj@gtk.org>
Tue, 2 May 2006 12:20:39 +0000 (12:20 +0000)
committerTim Janik <timj@src.gnome.org>
Tue, 2 May 2006 12:20:39 +0000 (12:20 +0000)
Tue May  2 14:18:25 2006  Tim Janik  <timj@gtk.org>

        * 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
ChangeLog.pre-2-12
glib/goption.c

index 2b551cc..b70598e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue May  2 14:18:25 2006  Tim Janik  <timj@gtk.org>
+
+       * 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  <behdad@gnome.org>
 
        * glib/guniprop.c: #include <stdlib.h>
index 2b551cc..b70598e 100644 (file)
@@ -1,3 +1,9 @@
+Tue May  2 14:18:25 2006  Tim Janik  <timj@gtk.org>
+
+       * 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  <behdad@gnome.org>
 
        * glib/guniprop.c: #include <stdlib.h>
index 446b6e3..a5026ec 100644 (file)
@@ -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)