From 1b55291feb1e44977193989cf488e0dcd4bf518b Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Sun, 26 Jul 2015 22:36:20 +0200 Subject: [PATCH] Fix secret_schema_unref detection of 0 refcount g_atomic_int_add (&schema->refs, -1); will return the value of 'refs' _before_ adding -1 to it, so checking this value for 0 to see if the refcount dropped to 0 after adding -1 is not going to work and will cause a leak. Using g_atomic_int_dec_and_test() fixes this problem as this will return TRUE when the value drops to 0 after being decremented. https://bugzilla.gnome.org/show_bug.cgi?id=756766 --- libsecret/secret-schema.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libsecret/secret-schema.c b/libsecret/secret-schema.c index 8bf7fa8..33522ef 100644 --- a/libsecret/secret-schema.c +++ b/libsecret/secret-schema.c @@ -355,16 +355,12 @@ _secret_schema_ref_if_nonstatic (const SecretSchema *schema) void secret_schema_unref (SecretSchema *schema) { - gint refs; - gint i; - g_return_if_fail (schema != NULL); + /* statically-allocated or invalid SecretSchema */ + g_return_if_fail (g_atomic_int_get (&schema->reserved) > 0); - refs = g_atomic_int_add (&schema->reserved, -1); - if (refs < 0) { - g_warning ("should not unreference a static or invalid SecretSchema"); - - } else if (refs == 0) { + if (g_atomic_int_dec_and_test (&schema->reserved)) { + gint i; g_free ((gpointer)schema->name); for (i = 0; i < G_N_ELEMENTS (schema->attributes); i++) g_free ((gpointer)schema->attributes[i].name);