[librest] Use g_hash_table_replace() in rest_params_add()



commit de21f49cda5554d1395d315c022c2f28d06ba778
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Wed Jun 4 16:51:33 2014 +0200

    Use g_hash_table_replace() in rest_params_add()
    
    rest_params_add() is currently using g_hash_table_insert() to add
    the passed in parameter in the parameter hash table. The key which
    is used is owned by the associated value.
    
    When using rest_params_add to replace an already existing parameter,
    the existing value will be freed with rest_param_unref().
    
    However, g_hash_table_insert() does not replace the key when it already
    exists in the hash table: "If the key already exists in the GHashTable
    its current value is replaced with the new value... If you supplied a
    key_destroy_func when creating the GHashTable, the passed key is freed
    using that function."
    
    This means that after replacing an already existing parameter, the
    corresponding key will still be the old one, which is now pointing
    at freed memory as the old value was freed.
    
    g_hash_table_replace() ensures that the key will still be valid, even
    when replacing existing parameters:
    "Inserts a new key and value into a GHashTable similar to
    g_hash_table_insert(). The difference is that if the key already exists
    in the GHashTable, it gets replaced by the new key."
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665716

 rest/rest-params.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/rest/rest-params.c b/rest/rest-params.c
index a5666fa..369215b 100644
--- a/rest/rest-params.c
+++ b/rest/rest-params.c
@@ -91,7 +91,7 @@ rest_params_add (RestParams *params, RestParam *param)
   g_return_if_fail (params);
   g_return_if_fail (param);
 
-  g_hash_table_insert (hash, (gpointer)rest_param_get_name (param), param);
+  g_hash_table_replace (hash, (gpointer)rest_param_get_name (param), param);
 }
 
 /**


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