[evolution-data-server/tintou/camel-folder] folder-thread: Clear all the structures and do not access GPtrArray fields



commit f7a08262993256b845ef9e5f903fb19ce6d7c6bf
Author: Corentin Noël <corentin noel collabora com>
Date:   Tue Jul 6 17:58:31 2021 +0200

    folder-thread: Clear all the structures and do not access GPtrArray fields
    
    Always use g_ptr_array_index to access it, add cleanup function to GPtrArray
    to make sure that no data is ever being leaked, use g_clear_pointer/object
    when possible to ensure no use-after-free.

 src/camel/camel-folder-thread.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
---
diff --git a/src/camel/camel-folder-thread.c b/src/camel/camel-folder-thread.c
index 4bdb2b094..411f6731d 100644
--- a/src/camel/camel-folder-thread.c
+++ b/src/camel/camel-folder-thread.c
@@ -541,7 +541,7 @@ thread_summary (CamelFolderThread *thread,
        id_table = g_hash_table_new_full (id_hash, id_equal, g_free, NULL);
        no_id_table = g_hash_table_new (NULL, NULL);
        for (i = 0; i < summary->len; i++) {
-               CamelMessageInfo *mi = summary->pdata[i];
+               CamelMessageInfo *mi = g_ptr_array_index (summary, i);
                CamelSummaryMessageID *message_id_copy, message_id;
                const GArray *references;
 
@@ -729,7 +729,7 @@ camel_folder_thread_messages_new (CamelFolder *folder,
        thread->folder = g_object_ref (folder);
 
        camel_folder_summary_prepare_fetch_all (camel_folder_get_folder_summary (folder), NULL);
-       thread->summary = summary = g_ptr_array_new ();
+       thread->summary = summary = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
 
        /* prefer given order from the summary order */
        if (!uids) {
@@ -739,12 +739,11 @@ camel_folder_thread_messages_new (CamelFolder *folder,
 
        for (i = 0; i < uids->len; i++) {
                CamelMessageInfo *info;
-               gchar *uid = uids->pdata[i];
+               const gchar *uid = g_ptr_array_index (uids, i);
 
                info = camel_folder_get_message_info (folder, uid);
                if (info)
                        g_ptr_array_add (summary, info);
-               /* FIXME: Check if the info is leaking */
        }
 
        if (fsummary)
@@ -801,15 +800,19 @@ camel_folder_thread_messages_apply (CamelFolderThread *thread,
 
        all = g_ptr_array_new ();
        table = g_hash_table_new (g_str_hash, g_str_equal);
-       for (i = 0; i < uids->len; i++)
-               g_hash_table_insert (table, uids->pdata[i], uids->pdata[i]);
+       for (i = 0; i < uids->len; i++) {
+               gchar *uid = g_ptr_array_index (uids, i);
+               g_hash_table_insert (table, uid, uid);
+       }
 
        add_present_rec (thread, table, all, thread->tree);
 
        /* add any new ones, in supplied order */
-       for (i = 0; i < uids->len; i++)
-               if (g_hash_table_lookup (table, uids->pdata[i]) && (info = camel_folder_get_message_info 
(thread->folder, uids->pdata[i])))
+       for (i = 0; i < uids->len; i++) {
+               const gchar *uid = g_ptr_array_index (uids, i);
+               if (g_hash_table_lookup (table, uid) && (info = camel_folder_get_message_info 
(thread->folder, uid)))
                        g_ptr_array_add (all, info);
+       }
 
        g_hash_table_destroy (table);
 
@@ -818,8 +821,7 @@ camel_folder_thread_messages_apply (CamelFolderThread *thread,
        thread->node_chunks = camel_memchunk_new (32, sizeof (CamelFolderThreadNode));
        thread_summary (thread, all);
 
-       g_ptr_array_free (thread->summary, TRUE);
-       thread->summary = all;
+       g_clear_pointer (&thread->summary, g_ptr_array_unref);
 }
 
 /**
@@ -851,15 +853,9 @@ camel_folder_thread_messages_unref (CamelFolderThread *thread)
                return;
        }
 
-       if (thread->folder) {
-               gint i;
-
-               for (i = 0; i < thread->summary->len; i++)
-                       g_clear_object (&thread->summary->pdata[i]);
-               g_ptr_array_free (thread->summary, TRUE);
-               g_object_unref (thread->folder);
-       }
-       camel_memchunk_destroy (thread->node_chunks);
+       g_clear_pointer (&thread->summary, g_ptr_array_unref);
+       g_clear_object (&thread->folder);
+       g_clear_pointer (&thread->node_chunks, camel_memchunk_destroy);
        g_free (thread);
 }
 


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