[milter-manager-commit] milter-manager/milter-manager at 479a050 [master] milter core: ensure to sort hash table keys (#139)

Back to archive index
Kentaro Hayashi null+****@clear*****
Wed Sep 11 10:14:44 JST 2019


Kentaro Hayashi	2019-09-11 10:14:44 +0900 (Wed, 11 Sep 2019)

  Revision: 479a0502926a118219ee7c6605c88989f6df9fc4
  https://github.com/milter-manager/milter-manager/commit/479a0502926a118219ee7c6605c88989f6df9fc4

  Message:
    milter core: ensure to sort hash table keys (#139)
    
    This commit fixes the following random error:
    (It was caused because GHashTable doesn't gurantee the order of inserted
    keys)
    
      Failure: test_inspect_hash_string_string
      <"{" "\"name1\" => \"value1\", " "\"name2\" => \"value2\"" "}" ==
    inspected>
       expected: <"{\"name1\" => \"value1\", \"name2\" => \"value2\"}">
       actual: <"{\"name2\" => \"value2\", \"name1\" => \"value1\"}">

  Modified files:
    milter/core/milter-utils.c

  Modified: milter/core/milter-utils.c (+13 -3)
===================================================================
--- milter/core/milter-utils.c    2019-09-10 15:10:51 +0900 (26c03acc)
+++ milter/core/milter-utils.c    2019-09-11 10:14:44 +0900 (bdddb988)
@@ -432,6 +432,11 @@ inspect_hash_string_string_element (gpointer _key, gpointer _value,
     g_string_append(inspected, ", ");
 }
 
+static gint compare_hash_key(gconstpointer a, gconstpointer b)
+{
+  return g_strcmp0((gchar*)a, (gchar*)b);
+}
+
 gchar *
 milter_utils_inspect_hash_string_string (GHashTable *hash)
 {
@@ -439,9 +444,14 @@ milter_utils_inspect_hash_string_string (GHashTable *hash)
 
     inspected = g_string_new("{");
     if (g_hash_table_size(hash) > 0) {
-        g_hash_table_foreach(hash,
-                             inspect_hash_string_string_element,
-                             inspected);
+        GList *key_list = g_hash_table_get_keys(hash);
+        key_list = g_list_sort(key_list, compare_hash_key);
+        const GList *node;
+        for (node = key_list; node; node = g_list_next(node)) {
+            gpointer value = g_hash_table_lookup(hash, node->data);
+            inspect_hash_string_string_element(node->data, value, inspected);
+        }
+        g_list_free(key_list);
         g_string_truncate(inspected, inspected->len - strlen(", "));
     }
     g_string_append(inspected, "}");
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/milter-manager-commit/attachments/20190911/78bd5ea9/attachment-0001.html>


More information about the milter-manager-commit mailing list
Back to archive index