[glib: 1/5] gresource: Assert that resource has correct refcount when unregistering




commit 86927c39e01d7d8ae1e520727db41b1402580e1a
Author: Philip Withnall <pwithnall endlessos org>
Date:   Tue May 10 15:53:19 2022 +0100

    gresource: Assert that resource has correct refcount when unregistering
    
    This should fix a scan-build warning that `resource` is
    used-after-freeing in the final `g_resource_unref()` call in
    `g_static_resource_fini()`, as `g_resources_unregister_unlocked()` has
    already unreffed it.
    
    In reality, each resource has two strong refs on it while active, so the
    second unref is correct.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 gio/gresource.c | 4 ++++
 1 file changed, 4 insertions(+)
---
diff --git a/gio/gresource.c b/gio/gresource.c
index 45ca92b1fc..921a029106 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -1444,6 +1444,10 @@ g_static_resource_fini (GStaticResource *static_resource)
   resource = g_atomic_pointer_get (&static_resource->resource);
   if (resource)
     {
+      /* There should be at least two references to the resource now: one for
+       * static_resource->resource, and one in the registered_resources list. */
+      g_assert (g_atomic_int_get (&resource->ref_count) >= 2);
+
       g_atomic_pointer_set (&static_resource->resource, NULL);
       g_resources_unregister_unlocked (resource);
       g_resource_unref (resource);


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