Parser leaks memory on inputs with multiple root nodes
json_parser_load_from_stream (json_parser_load) leaks memory when the input data contains multiple root nodes and is called in non-strict mode.
This error can be easily reproduced running json-glib-validate with input strings like e.g.:
[][]
{}{}
[][]{}{}
The leaks can be seen when running json-glib-validate with valgrind memcheck:
valgrind --tool=memcheck --leak-check=full ./json-glib-validate test.json
Memcheck, a memory error detector
Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
Command: _build/json-glib/json-glib-validate test.json
HEAP SUMMARY:
in use at exit: 75,492 bytes in 871 blocks
total heap usage: 1,206 allocs, 335 frees, 147,159 bytes allocated
88 (32 direct, 56 indirect) bytes in 1 blocks are definitely lost in loss record 660 of 799
at 0x484D953: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4ADF699: g_malloc0 (gmem.c:133)
by 0x486DC1E: json_node_alloc (json-node.c:132)
by 0x487242B: json_parse_array (json-parser.c:662)
by 0x4872FFA: json_parser_load (json-parser.c:1248)
by 0x4873B85: json_parser_load_from_stream (json-parser.c:1639)
by 0x109568: validate (json-glib-validate.c:72)
by 0x109568: main (json-glib-validate.c:169)
104 (56 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 760 of 799
at 0x4C56B2F: g_type_create_instance (gtype.c:1929)
by 0x4C3A1DF: g_object_new_internal.part.0 (gobject.c:2606)
by 0x4C3C165: g_object_new_internal (gobject.c:2603)
by 0x4C3C165: g_object_new_with_properties (gobject.c:2769)
by 0x4C3D010: g_object_new (gobject.c:2415)
by 0x49D08D7: _g_local_file_input_stream_new (glocalfileinputstream.c:132)
by 0x49C99C8: g_local_file_read (glocalfile.c:1395)
by 0x10954B: validate (json-glib-validate.c:60)
by 0x10954B: main (json-glib-validate.c:169)
560 (64 direct, 496 indirect) bytes in 2 blocks are definitely lost in loss record 789 of 799
at 0x484D953: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4ADF699: g_malloc0 (gmem.c:133)
by 0x486DC1E: json_node_alloc (json-node.c:132)
by 0x4871CF5: json_parse_object (json-parser.c:795)
by 0x4872FFA: json_parser_load (json-parser.c:1248)
by 0x4873B85: json_parser_load_from_stream (json-parser.c:1639)
by 0x109568: validate (json-glib-validate.c:72)
by 0x109568: main (json-glib-validate.c:169)
LEAK SUMMARY:
definitely lost: 152 bytes in 4 blocks
indirectly lost: 600 bytes in 11 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 70,212 bytes in 789 blocks
suppressed: 0 bytes in 0 blocks
Reachable blocks (those to which a pointer was found) are not shown.
To see them, rerun with: --leak-check=full --show-leak-kinds=all
For lists of detected and suppressed errors, rerun with: -s
ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
Version of JSON-GLib: 1.9.2. Issue still present on latest on main branch (1.9.2-24-g3f1a822)
JSON data that produced the bug (if any): [][]{}{}