[gegl] json: fix memory lifecycles in json_op_register_type_for_file



commit ffa6b1c60bf1e421faf5e65da3c9bfa0bc1eb7fd
Author: Andrzej Hunt <andrzej ahunt org>
Date:   Sat Aug 21 18:19:13 2021 +0200

    json: fix memory lifecycles in json_op_register_type_for_file
    
    - parser is created but never unref'd - we fix this leak with an unref.
    - The entire tree is copied (copy of root node), but we only use the root
      object - the copied root node is being leaked. We fix this leak by directly
      copying the root object, skipping the unnecessary root node copy.
    
    I think these leaks have existed since the initial version:
      4f502d213 (meta-json: Can set up initial proper, 2014-12-28)
    
    LSAN output from the root node leak:
    
    Direct leak of 64 byte(s) in 2 object(s) allocated from:
        #0 0x5e874d in malloc 
/home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
        #1 0x7f65023600c8 in g_malloc (/usr/lib64/libglib-2.0.so.0+0x580c8)
        #2 0x7f6502378da5 in g_slice_alloc (/usr/lib64/libglib-2.0.so.0+0x70da5)
        #3 0x7f6502379258 in g_slice_alloc0 (/usr/lib64/libglib-2.0.so.0+0x71258)
        #4 0x7f6503a993ad in json_node_alloc 
/usr/src/debug/json-glib-1.4.4-lp152.2.5.x86_64/build/../json-glib/json-node.c:127
        #5 0x7f6503a99e6e in json_node_copy 
/usr/src/debug/json-glib-1.4.4-lp152.2.5.x86_64/build/../json-glib/json-node.c:417
        #6 0x7f64e8d596dc in json_op_register_type_for_file 
/home/ahunt/git/gegl/_build/../operations/core/json.c:554:31
        #7 0x7f64e8d5969a in load_file /home/ahunt/git/gegl/_build/../operations/core/json.c:614:5
        #8 0x7f6504466cac in gegl_datafiles_read_directories 
/home/ahunt/git/gegl/_build/../gegl/module/gegldatafiles.c
        #9 0x7f64e8d59667 in load_path /home/ahunt/git/gegl/_build/../operations/core/json.c:620:5
        #10 0x7f650237a40c in g_slist_foreach (/usr/lib64/libglib-2.0.so.0+0x7240c)
        #11 0x7f64e8d59633 in json_register_operations 
/home/ahunt/git/gegl/_build/../operations/core/json.c:627:3
        #12 0x7f64e8d595f9 in gegl_op_json_register_type 
/home/ahunt/git/gegl/_build/../operations/core/json.c:670:3
        #13 0x7f64e8d54dc5 in gegl_module_register 
/home/ahunt/git/gegl/_build/operations/core/module_core.c:36:3
        #14 0x7f650446704f in gegl_module_load /home/ahunt/git/gegl/_build/../gegl/module/geglmodule.c:142:9
        #15 0x7f650266c602 in g_type_module_use 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtypemodule.c:244
        #16 0x7f650266c6ee in g_type_module_use_plugin 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtypemodule.c:310
        #17 0x7f6502665ea2 in type_data_ref_Wm 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtype.c:1243
        #18 0x7f6502665ea2 in g_type_class_ref 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtype.c:2947
        #19 0x7f6504474368 in add_operations 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:140:27
        #20 0x7f6504474379 in add_operations 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:142:7
        #21 0x7f65044742f8 in gegl_operation_gtype_from_name 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:287:11
        #22 0x7f65044610b7 in gegl_node_set_op_class 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:1294:14
        #23 0x7f6504462c70 in gegl_node_local_set_property 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:338:11
        #24 0x7f6502649991 in object_set_property 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1469
        #25 0x7f6502649991 in g_object_new_internal 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1861
        #26 0x7f650264b54d in g_object_new_valist 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:2152
        #27 0x7f650264b8c8 in g_object_new 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1670
        #28 0x7f65044624df in gegl_node_get_pad_proxy 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:2304:18
        #29 0x7f650445f802 in gegl_node_get_input_proxy 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:2343:10
        #30 0xce3b27 in gimp_drawable_get_source_node /home/ahunt/git/gimp/app/core/gimpdrawable.c:1525:11
    
    LSAN output for the parser leak:
    
    Direct leak of 192 byte(s) in 2 object(s) allocated from:
        #0 0x5e87ad in malloc 
/home/abuild/rpmbuild/BUILD/llvm-12.0.0.src/build/../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
        #1 0x7f1712a600c8 in g_malloc (/usr/lib64/libglib-2.0.so.0+0x580c8)
        #2 0x7f1712a78da5 in g_slice_alloc (/usr/lib64/libglib-2.0.so.0+0x70da5)
        #3 0x7f1712a79258 in g_slice_alloc0 (/usr/lib64/libglib-2.0.so.0+0x71258)
        #4 0x7f1712d691d5 in g_type_create_instance 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtype.c:1849
        #5 0x7f1712d4964f in g_object_new_internal 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1827
        #6 0x7f1712d4ae2c in g_object_new_with_properties 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1995
        #7 0x7f1712d4b8f0 in g_object_new 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1667
        #8 0x7f16f9459665 in json_op_register_type_for_file 
/home/ahunt/git/gegl/_build/../operations/core/json.c:550:26
        #9 0x7f16f945964a in load_file /home/ahunt/git/gegl/_build/../operations/core/json.c:579:5
        #10 0x7f1714b66d6c in gegl_datafiles_read_directories 
/home/ahunt/git/gegl/_build/../gegl/module/gegldatafiles.c
        #11 0x7f16f9459617 in load_path /home/ahunt/git/gegl/_build/../operations/core/json.c:585:5
        #12 0x7f1712a7a40c in g_slist_foreach (/usr/lib64/libglib-2.0.so.0+0x7240c)
        #13 0x7f16f94595e3 in json_register_operations 
/home/ahunt/git/gegl/_build/../operations/core/json.c:592:3
        #14 0x7f16f94595a9 in gegl_op_json_register_type 
/home/ahunt/git/gegl/_build/../operations/core/json.c:635:3
        #15 0x7f16f9454d75 in gegl_module_register 
/home/ahunt/git/gegl/_build/operations/core/module_core.c:36:3
        #16 0x7f1714b6710f in gegl_module_load /home/ahunt/git/gegl/_build/../gegl/module/geglmodule.c:142:9
        #17 0x7f1712d6c602 in g_type_module_use 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtypemodule.c:244
        #18 0x7f1712d6c6ee in g_type_module_use_plugin 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtypemodule.c:310
        #19 0x7f1712d65ea2 in type_data_ref_Wm 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtype.c:1243
        #20 0x7f1712d65ea2 in g_type_class_ref 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gtype.c:2947
        #21 0x7f1714b74428 in add_operations 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:140:27
        #22 0x7f1714b74439 in add_operations 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:142:7
        #23 0x7f1714b743b8 in gegl_operation_gtype_from_name 
/home/ahunt/git/gegl/_build/../gegl/operation/gegl-operations.c:287:11
        #24 0x7f1714b61177 in gegl_node_set_op_class 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:1294:14
        #25 0x7f1714b62d30 in gegl_node_local_set_property 
/home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:338:11
        #26 0x7f1712d49991 in object_set_property 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1469
        #27 0x7f1712d49991 in g_object_new_internal 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1861
        #28 0x7f1712d4b54d in g_object_new_valist 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:2152
        #29 0x7f1712d4b8c8 in g_object_new 
/usr/src/debug/glib2-2.62.6-lp152.2.3.1.x86_64/build/../gobject/gobject.c:1670
        #30 0x7f1714b6259f in gegl_node_get_pad_proxy
        /home/ahunt/git/gegl/_build/../gegl/graph/gegl-node.c:2304:18

 operations/core/json.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
---
diff --git a/operations/core/json.c b/operations/core/json.c
index 05c57d716..f5a0a8aea 100644
--- a/operations/core/json.c
+++ b/operations/core/json.c
@@ -551,12 +551,11 @@ json_op_register_type_for_file (GTypeModule *type_module, const gchar *filepath)
     const gboolean success = json_parser_load_from_file(parser, filepath, NULL);
 
     if (success) {
-        JsonNode *root_node = json_node_copy (json_parser_get_root (parser));
-        JsonObject *root = json_node_get_object (root_node);
+        JsonObject *root = json_node_dup_object(json_parser_get_root(parser));
         const gchar *name;
         gchar *type_name;
 
-        g_assert(root_node);
+        g_assert(root);
 
         name = metadata_get_property(root, "name");
         type_name = (name) ? component2gtypename(name) : component2gtypename(filepath);
@@ -564,7 +563,7 @@ json_op_register_type_for_file (GTypeModule *type_module, const gchar *filepath)
         g_free(type_name);
     }
 
-//    g_object_unref(parser);
+    g_object_unref(parser);
     return ret;
 }
 


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