[libegg] EggSpreadTable: forall(): Avoid use of invalid GList items.



commit e40ea6f0382a427cadba892ad458e2444ecc43ce
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Nov 8 15:58:48 2010 +0100

    EggSpreadTable: forall(): Avoid use of invalid GList items.
    
    	* glom/utility_widgets/eggspreadtable/eggspreadtable.c
    	(egg_spread_table_forall):
    	Use (and later free) a copy of the list, because the callback could call
    	egg_spread_table_remove() which would change the list, causing us to use a
    	list->next that is no longer valid. This fixes a problem found by valgrind.
    	I don't know why other Gtk containers don't need to do this, though their
    	code seems to do the same thing.

 libegg/spreadtable/eggspreadtable.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
---
diff --git a/libegg/spreadtable/eggspreadtable.c b/libegg/spreadtable/eggspreadtable.c
index 2e9170e..3ee48db 100644
--- a/libegg/spreadtable/eggspreadtable.c
+++ b/libegg/spreadtable/eggspreadtable.c
@@ -864,10 +864,19 @@ egg_spread_table_forall (GtkContainer *container,
 {
   EggSpreadTable        *table = EGG_SPREAD_TABLE (container);
   EggSpreadTablePrivate *priv = table->priv;
-  GList                 *list;
+  GList                 *list = NULL;
 
-  for (list = priv->children; list; list = list->next)
-    (* callback) ((GtkWidget *)list->data, callback_data);
+  /* We use a copy of the list,
+   * because the callback could call egg_spread_table_remove(),
+   * which would change the list, causing us to use a list->next that
+   * is no longer valid.
+   */
+  for (list = g_list_copy(priv->children); list; list = list->next)
+  {
+    (* callback) ((GtkWidget*) list->data, callback_data);
+  }
+
+  g_list_free(list);
 }
 
 static GType



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]