summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gmail.com>2021-10-09 16:07:06 +0000
committerEmmanuele Bassi <ebassi@gmail.com>2021-10-09 16:07:06 +0000
commita64aff2b4ae07e86f4f0fa3f3e9c17d4214f5e92 (patch)
treebf90ad9fd203aa5e727ff0c9c191490e33b1753d
parent2472b804aa5071e49d93d5a6453a27e6384b4a82 (diff)
parent40996bfad16896b6e715c1d1bad5f72ec3125021 (diff)
downloadjson-glib-a64aff2b4ae07e86f4f0fa3f3e9c17d4214f5e92.tar.gz
Merge branch 'safe-node-init' into 'master'
set node->data pointer to null when unset See merge request GNOME/json-glib!37
-rw-r--r--json-glib/json-node.c9
-rw-r--r--json-glib/tests/node.c17
2 files changed, 20 insertions, 6 deletions
diff --git a/json-glib/json-node.c b/json-glib/json-node.c
index 2737453..1e573eb 100644
--- a/json-glib/json-node.c
+++ b/json-glib/json-node.c
@@ -144,18 +144,15 @@ json_node_unset (JsonNode *node)
switch (node->type)
{
case JSON_NODE_OBJECT:
- if (node->data.object)
- json_object_unref (node->data.object);
+ g_clear_pointer (&(node->data.object), json_object_unref);
break;
case JSON_NODE_ARRAY:
- if (node->data.array)
- json_array_unref (node->data.array);
+ g_clear_pointer (&(node->data.array), json_array_unref);
break;
case JSON_NODE_VALUE:
- if (node->data.value)
- json_value_unref (node->data.value);
+ g_clear_pointer (&(node->data.value), json_value_unref);
break;
case JSON_NODE_NULL:
diff --git a/json-glib/tests/node.c b/json-glib/tests/node.c
index 80beb78..7d536f1 100644
--- a/json-glib/tests/node.c
+++ b/json-glib/tests/node.c
@@ -44,6 +44,22 @@ test_init_string (void)
json_node_free (node);
}
+/* Test that reinit used node */
+static void
+test_node_reinit (void)
+{
+ JsonNode *node = NULL;
+ JsonArray *array = NULL;
+
+ array = json_array_new ();
+ node = json_node_init_array (json_node_alloc (), array);
+
+ json_node_init_int (node, 1);
+
+ json_node_free (node);
+ json_array_unref (array);
+}
+
static void
test_copy_null (void)
{
@@ -556,6 +572,7 @@ main (int argc,
g_test_add_func ("/nodes/init/boolean", test_init_boolean);
g_test_add_func ("/nodes/init/string", test_init_string);
g_test_add_func ("/nodes/init/null", test_null);
+ g_test_add_func ("/nodes/init/reinit", test_node_reinit);
g_test_add_func ("/nodes/copy/null", test_copy_null);
g_test_add_func ("/nodes/copy/value", test_copy_value);
g_test_add_func ("/nodes/copy/object", test_copy_object);