fix json_object_put() to return 1 for freed scalars and empty containers

The iterative rewrite in 17328a67 made json_object_put() return 0 for freed scalars, strings and empty containers, contradicting the documented contract in json_object.h. Distinguish "freed" from "still referenced" in _json_object_put_maybe_free() and add regression tests.
This commit is contained in:
Arran Cudbard-Bell
2026-07-14 10:28:04 -04:00
parent e65444a258
commit 69be99ef8e
3 changed files with 70 additions and 16 deletions
+27 -1
View File
@@ -81,12 +81,36 @@ static void test_nesting_with_user_delete(void)
1, malloc(8192)
};
jso = json_object_new_object();
jso = json_object_new_object();
json_object_set_userdata(jso, &userdata_val, user_delete_test);
json_object_object_add(jso, "somekey", json_object_new_string("foo"));
json_object_put(jso);
}
/*
* Check that json_object_put() returns 1 whenever the object is freed,
* including for scalars and empty containers, and 0 only when a
* reference remains.
*/
static void test_put_return_values(void)
{
json_object *jso;
printf("put(empty object) returned %d\n", json_object_put(json_object_new_object()));
printf("put(empty array) returned %d\n", json_object_put(json_object_new_array()));
printf("put(string) returned %d\n", json_object_put(json_object_new_string("foo")));
printf("put(int) returned %d\n", json_object_put(json_object_new_int(42)));
jso = json_object_new_object();
json_object_object_add(jso, "somekey", json_object_new_string("foo"));
printf("put(non-empty object) returned %d\n", json_object_put(jso));
jso = json_object_new_object();
json_object_get(jso);
printf("put(still-referenced object) returned %d\n", json_object_put(jso));
printf("put(last reference) returned %d\n", json_object_put(jso));
}
int main(int argc, char **argv)
{
char *str;
@@ -110,5 +134,7 @@ int main(int argc, char **argv)
test_nesting_with_user_delete();
test_put_return_values();
return EXIT_SUCCESS;
}