dcerpc: remove use-after-free (found by clang).
[metze/wireshark/wip.git] / epan / stat_tap_ui.c
index c76ab451fa914731b2a0f6a22bec3a441dfe393a..258c77e8a5b3ec979fc63fb0c2ff3d6c16f41402 100644 (file)
@@ -40,7 +40,7 @@ typedef struct _stat_cmd_arg {
     void* userdata;
 } stat_cmd_arg;
 
-static GList *stat_cmd_arg_list=NULL;
+static wmem_list_t *stat_cmd_arg_list=NULL;
 
 /* structure to keep track of what stats have been specified on the
    command line.
@@ -55,6 +55,12 @@ static GSList *stats_requested = NULL;
  * Function called from stat to register the stat's command-line argument
  * and initialization routine
  * ********************************************************************** */
+static gint
+search_duplicate(gconstpointer a, gconstpointer b)
+{
+    return strcmp(((const stat_cmd_arg *)a)->cmd, (const char *)b);
+}
+
 static gint
 sort_by_name(gconstpointer a, gconstpointer b)
 {
@@ -66,11 +72,19 @@ register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
 {
     stat_cmd_arg *newsca;
 
-    newsca=(stat_cmd_arg *)g_malloc(sizeof(stat_cmd_arg));
-    newsca->cmd=ui->cli_string;
+    if (stat_cmd_arg_list == NULL)
+        stat_cmd_arg_list = wmem_list_new(wmem_epan_scope());
+
+    /* Key is already present */
+    if (wmem_list_find_custom(stat_cmd_arg_list, ui->cli_string, search_duplicate))
+        return;
+
+    newsca = wmem_new(wmem_epan_scope(), stat_cmd_arg);
+    newsca->cmd= wmem_strdup(wmem_epan_scope(), ui->cli_string);
     newsca->func=ui->tap_init_cb;
     newsca->userdata=userdata;
-    stat_cmd_arg_list=g_list_insert_sorted(stat_cmd_arg_list, newsca, sort_by_name);
+
+    wmem_list_insert_sorted(stat_cmd_arg_list, newsca, sort_by_name);
 }
 
 /* **********************************************************************
@@ -79,17 +93,20 @@ register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
 gboolean
 process_stat_cmd_arg(char *optstr)
 {
-    GList *entry;
+    wmem_list_frame_t *entry;
     stat_cmd_arg *sca;
     stat_requested *tr;
 
-    for(entry=g_list_last(stat_cmd_arg_list);entry;entry=g_list_previous(entry)){
-        sca=(stat_cmd_arg *)entry->data;
-        if(!strncmp(sca->cmd,optstr,strlen(sca->cmd))){
+    /* The strings "ipx" or "ipv6" must be tested before "ip" to select the
+      right tap so the sorting does matter.  And it's also why the list is
+      walked backwards */
+    for (entry = wmem_list_tail(stat_cmd_arg_list); entry; entry = wmem_list_frame_prev(entry)) {
+        sca = (stat_cmd_arg*)wmem_list_frame_data(entry);
+        if(!strncmp(sca->cmd, optstr, strlen(sca->cmd))) {
             tr=(stat_requested *)g_malloc(sizeof (stat_requested));
             tr->sca = sca;
             tr->arg=g_strdup(optstr);
-            stats_requested=g_slist_append(stats_requested, tr);
+            stats_requested = g_slist_append(stats_requested, tr);
             return TRUE;
         }
     }
@@ -99,16 +116,16 @@ process_stat_cmd_arg(char *optstr)
 /* **********************************************************************
  * Function to list all possible tap command-line arguments
  * ********************************************************************** */
+static void
+list_stat_cmd_args_func(gpointer data, gpointer userdata _U_)
+{
+    fprintf(stderr,"     %s\n", ((stat_cmd_arg*)data)->cmd);
+}
+
 void
 list_stat_cmd_args(void)
 {
-    GList *entry;
-    stat_cmd_arg *sca;
-
-    for(entry=stat_cmd_arg_list;entry;entry=g_list_next(entry)){
-        sca=(stat_cmd_arg *)entry->data;
-        fprintf(stderr,"     %s\n",sca->cmd);
-    }
+    wmem_list_foreach(stat_cmd_arg_list, list_stat_cmd_args_func, NULL);
 }
 
 /* **********************************************************************
@@ -122,34 +139,33 @@ start_requested_stats(void)
     while(stats_requested){
         sr=(stat_requested *)stats_requested->data;
         (*sr->sca->func)(sr->arg,sr->sca->userdata);
+        stats_requested=g_slist_remove(stats_requested, sr);
         g_free(sr->arg);
         g_free(sr);
-        stats_requested=g_slist_remove(stats_requested, sr);
     }
 }
 
-static GSList *registered_stat_tables = NULL;
+static wmem_tree_t *registered_stat_tables = NULL;
 
-static gint
-insert_sorted_by_cli_string(gconstpointer aparam, gconstpointer bparam)
+void register_stat_tap_table_ui(stat_tap_table_ui *ui)
 {
-    const new_stat_tap_ui *a = (const new_stat_tap_ui *)aparam;
-    const new_stat_tap_ui *b = (const new_stat_tap_ui *)bparam;
+    if (registered_stat_tables == NULL)
+        registered_stat_tables = wmem_tree_new(wmem_epan_scope());
 
-    return g_ascii_strcasecmp(a->cli_string, b->cli_string);
+    wmem_tree_insert_string(registered_stat_tables, ui->cli_string, ui, 0);
 }
 
-void register_new_stat_tap_ui(new_stat_tap_ui *ui)
+stat_tap_table_ui *new_stat_tap_by_name(const char *name)
 {
-    registered_stat_tables = g_slist_insert_sorted(registered_stat_tables, ui, insert_sorted_by_cli_string);
+    return (stat_tap_table_ui *) wmem_tree_lookup_string(registered_stat_tables, name, 0);
 }
 
-void new_stat_tap_iterate_tables(GFunc func, gpointer user_data)
+void new_stat_tap_iterate_tables(wmem_foreach_func func, gpointer user_data)
 {
-    g_slist_foreach(registered_stat_tables, func, user_data);
+    wmem_tree_foreach(registered_stat_tables, func, user_data);
 }
 
-void new_stat_tap_get_filter(new_stat_tap_ui* new_stat, const char *opt_arg, const char **filter, char** err)
+void new_stat_tap_get_filter(stat_tap_table_ui* new_stat, const char *opt_arg, const char **filter, char** err)
 {
     guint len = (guint) strlen(new_stat->cli_string);
     *filter=NULL;
@@ -163,8 +179,8 @@ void new_stat_tap_get_filter(new_stat_tap_ui* new_stat, const char *opt_arg, con
         }
     }
 
-    if (new_stat->new_stat_filter_check_cb)
-        new_stat->new_stat_filter_check_cb(opt_arg, filter, err);
+    if (new_stat->stat_filter_check_cb)
+        new_stat->stat_filter_check_cb(opt_arg, filter, err);
 }
 
 stat_tap_table* new_stat_tap_init_table(const char *name, int num_fields, int num_elements,
@@ -184,7 +200,7 @@ stat_tap_table* new_stat_tap_init_table(const char *name, int num_fields, int nu
     return new_table;
 }
 
-void new_stat_tap_add_table(new_stat_tap_ui* new_stat, stat_tap_table* table)
+void new_stat_tap_add_table(stat_tap_table_ui* new_stat, stat_tap_table* table)
 {
     if (new_stat->tables == NULL)
         new_stat->tables = g_array_new(FALSE, TRUE, sizeof(stat_tap_table*));
@@ -233,7 +249,7 @@ void new_stat_tap_set_field_data(stat_tap_table *stat_table, guint table_index,
     field_value[field_index] = *field_data;
 }
 
-void reset_stat_table(new_stat_tap_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
+void reset_stat_table(stat_tap_table_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data)
 {
     guint i = 0;
     stat_tap_table *stat_table;
@@ -251,7 +267,7 @@ void reset_stat_table(new_stat_tap_ui* new_stat, new_stat_tap_gui_reset_cb gui_c
     }
 }
 
-void free_stat_tables(new_stat_tap_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
+void free_stat_tables(stat_tap_table_ui* new_stat, new_stat_tap_gui_free_cb gui_callback, void *callback_data)
 {
     guint i = 0, element, field_index;
     stat_tap_table *stat_table;