[gnome-keyring/wip/header-order] secret-store: Sort fields alphabetically before outputting



commit 3091bf66c07f8df66876ecc7832371170743b052
Author: Iain Lane <iainl gnome org>
Date:   Wed Jan 30 14:27:12 2019 +0000

    secret-store: Sort fields alphabetically before outputting
    
    The assumption that we'd get values out of a hash table in the same
    order we put them in stopped holding with GLib 2.59:
    
      ERROR:…/gnome-keyring/pkcs11/secret-store/test-secret-item.c:379:test_fields_attr:
      assertion failed: (memcmp (buffer, "name1\0value1\0name2\0value2", 26)
      == 0)
    
    Let's ensure a consistent order by sorting the fields before returning
    them.
    
    Closes #21.

 pkcs11/secret-store/gkm-secret-fields.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
---
diff --git a/pkcs11/secret-store/gkm-secret-fields.c b/pkcs11/secret-store/gkm-secret-fields.c
index 9cf74178..95558c10 100644
--- a/pkcs11/secret-store/gkm-secret-fields.c
+++ b/pkcs11/secret-store/gkm-secret-fields.c
@@ -214,9 +214,9 @@ gkm_secret_fields_serialize (CK_ATTRIBUTE_PTR attr,
                              GHashTable *fields,
                              const gchar *schema_name)
 {
-       GHashTableIter iter;
+       GList *l, *keys;
        gboolean saw_schema = FALSE;
-       gpointer key;
+       const gchar *key;
        gpointer value;
        GString *result;
        CK_RV rv;
@@ -224,10 +224,15 @@ gkm_secret_fields_serialize (CK_ATTRIBUTE_PTR attr,
        g_assert (attr != NULL);
        g_assert (fields != NULL);
 
+       keys = g_hash_table_get_keys (fields);
+       keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
+
        if (!attr->pValue) {
                attr->ulValueLen = 0;
-               g_hash_table_iter_init (&iter, fields);
-               while (g_hash_table_iter_next (&iter, &key, &value)) {
+               for (l = keys; l != NULL; l = l->next) {
+                       key = (const gchar *) l->data;
+                       value = g_hash_table_lookup (fields, key);
+
                        if (g_str_equal (key, GKM_SECRET_FIELD_SCHEMA))
                                saw_schema = TRUE;
                        attr->ulValueLen += strlen (key);
@@ -239,12 +244,15 @@ gkm_secret_fields_serialize (CK_ATTRIBUTE_PTR attr,
                        attr->ulValueLen += strlen (schema_name);
                        attr->ulValueLen += 2;
                }
+               if (keys != NULL)
+                       g_list_free (keys);
                return CKR_OK;
        }
 
        result = g_string_sized_new (256);
-       g_hash_table_iter_init (&iter, fields);
-       while (g_hash_table_iter_next (&iter, &key, &value)) {
+       for (l = keys; l != NULL; l = l->next) {
+               key = (const gchar *) l->data;
+               value = g_hash_table_lookup (fields, key);
                if (g_str_equal (key, GKM_SECRET_FIELD_SCHEMA))
                        saw_schema = TRUE;
                g_string_append (result, key);
@@ -261,6 +269,8 @@ gkm_secret_fields_serialize (CK_ATTRIBUTE_PTR attr,
 
        rv = gkm_attribute_set_data (attr, result->str, result->len);
        g_string_free (result, TRUE);
+       if (keys != NULL)
+       g_list_free (keys);
 
        return rv;
 }


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