From 5fccfed4f43a0b4cb7f6082c01d9ad3ab8230a5a Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sat, 20 Aug 2016 22:41:49 -0400 Subject: [PATCH] Issue #260: add a check to prevent trivial loops in the object tree, even though it is up to the callers to avoid doing so in more complex cases. --- json_object.c | 6 ++++++ tests/test1.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/json_object.c b/json_object.c index 5104be9..f657afe 100644 --- a/json_object.c +++ b/json_object.c @@ -459,6 +459,12 @@ int json_object_object_add_ex(struct json_object* jso, existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash); + + // The caller must avoid creating loops in the object tree, but do a + // quick check anyway to make sure we're not creating a trivial loop. + if (jso == val) + return -1; + if (!existing_entry) { const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? diff --git a/tests/test1.c b/tests/test1.c index fee799c..b093df7 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -205,6 +205,12 @@ int main(int argc, char **argv) printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); + int rc = json_object_object_add(my_object, "abc", my_object); + if (rc != -1) + { + printf("ERROR: able to successfully add object to itself!\n"); + fflush(stdout); + } json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); -- 2.49.0