From e781c26b2516d2131f9b830e174e5e2b41e91d81 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Sat, 16 Apr 2011 10:52:12 -0400 Subject: [PATCH] GTimeZone: fix non-threadsafe refcounting In the previous code, if the timezone was pulled out of the cache again just as the last reference was being dropped, the cache code will increase its refcount and return it while the unref code was freeing it. Protect against that. Note that this patch is not a straight cherry-pick of the one from master. It follows closer to the solution that was originally recommended in the bug and holds the mutex during every unref operation. Because we don't have the GTimeZoneMonitor changes involved here, it's a little bit more elegant (due to no early exit condition). Also, it's the stable release, and I have more confidence in it (even if it's probably slower). Closes #646435. --- glib/gtimezone.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glib/gtimezone.c b/glib/gtimezone.c index 902e2c3f9..e8826822a 100644 --- a/glib/gtimezone.c +++ b/glib/gtimezone.c @@ -164,11 +164,10 @@ g_time_zone_unref (GTimeZone *tz) { g_assert (tz->ref_count > 0); + G_LOCK(time_zones); if (g_atomic_int_dec_and_test (&tz->ref_count)) { - G_LOCK(time_zones); g_hash_table_remove (time_zones, tz->name); - G_UNLOCK(time_zones); if (tz->zoneinfo) g_buffer_unref (tz->zoneinfo); @@ -177,6 +176,7 @@ g_time_zone_unref (GTimeZone *tz) g_slice_free (GTimeZone, tz); } + G_UNLOCK(time_zones); } /**