[gjs] arg: Set hash functions according to GI type



commit de5dd6541d2745817a618f35d2b27497d122766c
Author: Philip Chimento <philip endlessm com>
Date:   Mon Oct 31 18:43:21 2016 -0700

    arg: Set hash functions according to GI type
    
    Previously it was assumed that all GHashTable keys were strings when
    constructing a hash table from a JS object. This causes a segfault when
    you use, for example, GINT_TO_POINTER(-1) as a key, since g_str_hash will
    try to dereference the pointer.
    
    Sadly this does not fix any tests by itself, but it is a prerequisite.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773763

 gi/arg.cpp |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 87e68f8..4b293f6 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -327,6 +327,20 @@ gjs_array_to_g_list(JSContext   *context,
     return true;
 }
 
+static GHashTable *
+create_hash_table_for_key_type(GITypeInfo  *key_param_info)
+{
+    /* Don't use key/value destructor functions here, because we can't
+     * construct correct ones in general if the value type is complex.
+     * Rely on the type-aware g_argument_release functions. */
+
+    GITypeTag key_type = g_type_info_get_tag(key_param_info);
+
+    if (key_type == GI_TYPE_TAG_UTF8 || key_type == GI_TYPE_TAG_FILENAME)
+        return g_hash_table_new(g_str_hash, g_str_equal);
+    return g_hash_table_new(g_direct_hash, g_direct_equal);
+}
+
 static bool
 gjs_object_to_g_hash(JSContext   *context,
                      JS::Value    hash_value,
@@ -360,10 +374,7 @@ gjs_object_to_g_hash(JSContext   *context,
     if (!ids)
         return false;
 
-    /* Don't use key/value destructor functions here, because we can't
-     * construct correct ones in general if the value type is complex.
-     * Rely on the type-aware g_argument_release functions. */
-    result = g_hash_table_new(g_str_hash, g_str_equal);
+    result = create_hash_table_for_key_type(key_param_info);
 
     JS::RootedValue key_js(context), val_js(context);
     for (id_ix = 0, id_len = ids.length(); id_ix < id_len; id_ix++) {


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