In atspi_match_rule_get_attributes, copy the hash rather than ref it
authorMike Gorse <mgorse@novell.com>
Tue, 17 May 2011 19:00:40 +0000 (14:00 -0500)
committerMike Gorse <mgorse@novell.com>
Tue, 17 May 2011 19:00:40 +0000 (14:00 -0500)
It is not safe to ref a hash marked (transfer none) and expect its elements
to remain, since the caller will assume that it owns the elements and may
deallocate them (this is done by pygobject).  It would also be problematic
to simply mark the hash (transfer full), since we need to either assume that
the caller passed a destroy function when creating the hash (pygobject does
not) or deallocate the values ourselves when the match rule is finalized
(which requires making assumptions about the correct free function needed to
deallocate the values that the caller allocated).  So it seems that the only
safe thing to do is to copy the hash.

atspi/atspi-matchrule.c

index 80783d1..76415c2 100644 (file)
@@ -60,6 +60,9 @@ atspi_match_rule_finalize (GObject *object)
   if (rule->interfaces)
     g_array_free (rule->interfaces, TRUE);
 
+  if (rule->attributes)
+    g_hash_table_unref (rule->attributes);
+
   G_OBJECT_CLASS (atspi_match_rule_parent_class)->finalize (object);
 }
 
@@ -119,8 +122,19 @@ atspi_match_rule_new (AtspiStateSet *states,
   rule->statematchtype = statematchtype;
 
   if (attributes)
-    rule->attributes = g_hash_table_ref (attributes);
-    rule->attributematchtype = attributematchtype;
+  {
+    GHashTableIter hash_table_iter;
+    gchar *key, *value;
+    rule->attributes = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                              (GDestroyNotify) g_free,
+                                              (GDestroyNotify) g_free);
+    g_hash_table_iter_init (&hash_table_iter, attributes);
+            while (g_hash_table_iter_next (&hash_table_iter, (gpointer *)&key,
+                   (gpointer *)&value))
+      g_hash_table_insert (rule->attributes, g_strdup (key), g_strdup (value));
+  } else
+    rule->attributes = NULL;
+  rule->attributematchtype = attributematchtype;
 
   if (interfaces)
     rule->interfaces = g_array_ref (interfaces);
@@ -147,7 +161,7 @@ atspi_match_rule_new (AtspiStateSet *states,
 }
 
 static void
-append_entry (gpointer *key, gpointer *val, gpointer data)
+append_entry (gpointer key, gpointer val, gpointer data)
 {
   DBusMessageIter *iter = data;
   DBusMessageIter iter_entry;