[gjs: 2/3] function: Unref the allocated trampoline if returning early




commit 392828eec4824b335e6050db4b3167184da341ea
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Sep 2 20:18:25 2020 +0200

    function: Unref the allocated trampoline if returning early
    
    When creating the trampoline, if we have an early return, we need to
    cleanup the allocated memory or we'd leak.
    
    This happens for example during the GObjectClass test "gracefully bails out
    when overriding an unsupported vfunc type", where the trampoline
    creation fails, but in such case we never free the allocated memory

 gi/function.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 3973c6da8..1b065b598 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -102,9 +102,10 @@ gjs_callback_trampoline_unref(GjsCallbackTrampoline *trampoline)
 
     trampoline->ref_count--;
     if (trampoline->ref_count == 0) {
-        g_closure_unref(trampoline->js_function);
-        g_callable_info_free_closure(trampoline->info, trampoline->closure);
-        g_base_info_unref( (GIBaseInfo*) trampoline->info);
+        g_clear_pointer(&trampoline->js_function, g_closure_unref);
+        if (trampoline->info && trampoline->closure)
+            g_callable_info_free_closure(trampoline->info, trampoline->closure);
+        g_clear_pointer(&trampoline->info, g_base_info_unref);
         g_free (trampoline->param_types);
         g_slice_free(GjsCallbackTrampoline, trampoline);
     }
@@ -588,6 +589,7 @@ GjsCallbackTrampoline* gjs_callback_trampoline_new(
                           is_vfunc ? "VFunc" : "Callback",
                           g_base_info_get_name(callable_info));
                 g_base_info_unref(interface_info);
+                gjs_callback_trampoline_unref(trampoline);
                 return NULL;
             }
             g_base_info_unref(interface_info);
@@ -605,6 +607,7 @@ GjsCallbackTrampoline* gjs_callback_trampoline_new(
                                   "length argument. This is not supported",
                                   is_vfunc ? "VFunc" : "Callback",
                                   g_base_info_get_name(callable_info));
+                        gjs_callback_trampoline_unref(trampoline);
                         return NULL;
                     }
 


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