fix cxJsonDestroy() not freeing uncompleted object member names

Wed, 01 Jan 2025 15:56:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Jan 2025 15:56:02 +0100
changeset 1075
0cc4b63a0ae0
parent 1074
e8e2813cdda6
child 1076
bb4da3255de3

fix cxJsonDestroy() not freeing uncompleted object member names

src/json.c file | annotate | diff | comparison | revisions
tests/test_json.c file | annotate | diff | comparison | revisions
--- a/src/json.c	Wed Jan 01 15:50:57 2025 +0100
+++ b/src/json.c	Wed Jan 01 15:56:02 2025 +0100
@@ -419,6 +419,10 @@
     }
     cxJsonValueFree(json->parsed);
     json->parsed = NULL;
+    if (json->uncompleted_member.name.ptr != NULL) {
+        cx_strfree_a(json->allocator, &json->uncompleted_member.name);
+        json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL};
+    }
 }
 
 int cxJsonFilln(CxJson *json, const char *buf, size_t size) {
--- a/tests/test_json.c	Wed Jan 01 15:50:57 2025 +0100
+++ b/tests/test_json.c	Wed Jan 01 15:56:02 2025 +0100
@@ -194,13 +194,18 @@
 }
 
 CX_TEST(test_json_token_wrongly_completed) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    const CxAllocator *alloc = &talloc.base;
+
     cxstring text = cx_str("{\"number\": 47110815!}");
     cxstring part1 = cx_strsubsl(text, 0, 16);
     cxstring part2 = cx_strsubs(text, 16);
 
-    CxJson json;
-    cxJsonInit(&json, NULL);
     CX_TEST_DO {
+        CxJson json;
+        cxJsonInit(&json, alloc);
+
         CxJsonStatus result;
         CxJsonValue *obj;
 
@@ -211,8 +216,11 @@
         result = cxJsonNext(&json, &obj);
         CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_NUMBER);
         CX_TEST_ASSERT(obj->type == CX_JSON_NOTHING);
+
+        cxJsonDestroy(&json);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
     }
-    cxJsonDestroy(&json);
+    cx_testing_allocator_destroy(&talloc);
 }
 
 CX_TEST(test_json_subsequent_fill) {

mercurial