aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCorey Farrell <git@cfware.com>2017-11-06 22:48:50 -0500
committerCorey Farrell <git@cfware.com>2017-12-13 14:03:58 -0500
commit9a1d9c88fce268b812453d0d23237ee805b9bc34 (patch)
treef437b3fadd130b71fc7feab75595fdcbc81b9476 /test
parent02dade46c0f860abf11f1b173b808ad502b44842 (diff)
downloadjansson-9a1d9c88fce268b812453d0d23237ee805b9bc34.zip
jansson-9a1d9c88fce268b812453d0d23237ee805b9bc34.tar.gz
jansson-9a1d9c88fce268b812453d0d23237ee805b9bc34.tar.bz2
json_pack: Enable more complete stealing of references.
Users of the "o" format have an expectation that the object reference will be stolen. Any error causes the collection process to end early. This patch causes json_pack and related functions to continue scanning the format and parameters so all references can be stolen to prevent leaks. This makes no attempt to continue processing if the format string is broken or missing. 'make check' still passes. Ran test_pack under valgrind and verified that the leaked reference is fixed. Added a test which uses refcounts to verify that the reference was correctly stolen after a NULL value error. Issue #135
Diffstat (limited to 'test')
-rw-r--r--test/suites/api/test_pack.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/test/suites/api/test_pack.c b/test/suites/api/test_pack.c
index 02631a0..fa44b8b 100644
--- a/test/suites/api/test_pack.c
+++ b/test/suites/api/test_pack.c
@@ -352,6 +352,15 @@ static void run_tests()
fail("json_pack failed to catch NULL key");
check_error(json_error_null_value, "NULL string argument", "<args>", 1, 2, 2);
+ /* NULL value followed by object still steals the object's ref */
+ value = json_incref(json_object());
+ if(json_pack_ex(&error, 0, "{s:s,s:o}", "badnull", NULL, "dontleak", value))
+ fail("json_pack failed to catch NULL value");
+ check_error(json_error_null_value, "NULL string argument", "<args>", 1, 4, 4);
+ if(value->refcount != (size_t)1)
+ fail("json_pack failed to steal reference after error.");
+ json_decref(value);
+
/* More complicated checks for row/columns */
if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
fail("json_pack failed to catch object as key");