Blob Blame Raw
From acd6f57e2121edcb6625ff02042e8c5b799b9d98 Mon Sep 17 00:00:00 2001
From: Michael Catanzaro <mcatanzaro@igalia.com>
Date: Sun, 21 Jul 2019 13:38:40 -0500
Subject: [PATCH] Don't leave dangling pointers in the GcalEvent cache

The cache has an unowned pointer to the GcalEvent's UID. We need to
update the cache with the GcalEvent's new UID to avoid use-after-free.

Fixes #434
---
 src/gcal-event.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/gcal-event.c b/src/gcal-event.c
index e8eada98..07c74222 100644
--- a/src/gcal-event.c
+++ b/src/gcal-event.c
@@ -259,13 +259,19 @@ gcal_event_update_uid_internal (GcalEvent *self)
 {
   ECalComponentId *id;
   const gchar *source_id;
+  gboolean should_update_cache = FALSE;
 
   /* Setup event uid */
   source_id = self->source ? e_source_get_uid (self->source) : "";
   id = e_cal_component_get_id (self->component);
 
-  /* Clear the previous uid */
-  g_clear_pointer (&self->uid, g_free);
+  if (self->uid != NULL)
+    {
+      should_update_cache = TRUE;
+      g_debug ("Removing '%s' (%p) from cache", self->uid, self);
+      g_hash_table_remove (event_cache, self->uid);
+      g_free (self->uid);
+    }
 
   if (id->rid != NULL)
     {
@@ -281,6 +287,12 @@ gcal_event_update_uid_internal (GcalEvent *self)
                                    id->uid);
     }
 
+  if (should_update_cache)
+    {
+      g_debug ("Adding %s to the cache", self->uid);
+      g_hash_table_insert (event_cache, self->uid, self);
+    }
+
   e_cal_component_free_id (id);
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_UID]);
 }
-- 
2.21.0