fix regression in cx_kvl_map_put() - after recent refactoring it returned the wrong value

Sun, 14 Dec 2025 14:29:27 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Dec 2025 14:29:27 +0100
changeset 1600
a1a728d7ebfd
parent 1599
aa55109f01d0
child 1601
e47078681a6f

fix regression in cx_kvl_map_put() - after recent refactoring it returned the wrong value

src/json.c file | annotate | diff | comparison | revisions
src/kv_list.c file | annotate | diff | comparison | revisions
src/map.c file | annotate | diff | comparison | revisions
tests/test_json.c file | annotate | diff | comparison | revisions
--- a/src/json.c	Sun Dec 14 14:15:26 2025 +0100
+++ b/src/json.c	Sun Dec 14 14:29:27 2025 +0100
@@ -453,7 +453,7 @@
 }
 
 static CxJsonObject json_create_object_map(const CxAllocator *allocator) {
-    CxMap *map = cxKvListCreateAsMap(allocator, NULL, CX_STORE_POINTERS);
+    CxMap *map = cxKvListCreateAsMap(allocator, (cx_compare_func) cxJsonCompare, CX_STORE_POINTERS);
     if (map == NULL) return NULL; // LCOV_EXCL_LINE
     // TODO: fix the specification of the compare function
     map->collection.cmpfunc = (cx_compare_func) cxJsonCompare;
@@ -1502,6 +1502,7 @@
     if (source == NULL || source->type == CX_JSON_NOTHING) {
         return &cx_json_value_nothing;
     }
+    if (allocator == NULL) allocator = cxDefaultAllocator;
 
 #define return_value(v) { \
         CxJsonValue *ret = v; \
@@ -1524,7 +1525,7 @@
                 return NULL;
                 // LCOV_EXCL_STOP
             }
-            return obj;
+            return_value(obj);
         }
         case CX_JSON_ARRAY: {
             const size_t elem_count = source->array.data_size;
@@ -1548,7 +1549,7 @@
                 }
                 arr->array.data[i] = e;
             }
-            return arr;
+            return_value(arr);
         }
         case CX_JSON_STRING:
             return_value(cxJsonCreateString(allocator, source->string));
--- a/src/kv_list.c	Sun Dec 14 14:15:26 2025 +0100
+++ b/src/kv_list.c	Sun Dec 14 14:29:27 2025 +0100
@@ -381,7 +381,8 @@
     CxHashKey *key_ptr = cx_kv_list_loc_key(kv_list, node_data);
     *key_ptr = *map_entry.key;
 
-    return map_entry;
+    // we must return an entry that points to the node data!
+    return (CxMapEntry ){key_ptr, node_data};
 }
 
 static void *cx_kvl_iter_current_entry(const void *it) {
--- a/src/map.c	Sun Dec 14 14:15:26 2025 +0100
+++ b/src/map.c	Sun Dec 14 14:29:27 2025 +0100
@@ -114,7 +114,9 @@
 }
 
 void *cx_map_emplace(CxMap *map, CxHashKey key) {
-    return map->cl->put(map, key, NULL).value;
+    const CxMapEntry entry = map->cl->put(map, key, NULL);
+    if (entry.key == NULL) return NULL;
+    return entry.value;
 }
 
 void *cx_map_get(const CxMap *map, CxHashKey key) {
--- a/tests/test_json.c	Sun Dec 14 14:15:26 2025 +0100
+++ b/tests/test_json.c	Sun Dec 14 14:29:27 2025 +0100
@@ -1676,8 +1676,7 @@
     }
     
     CX_TEST_DO {
-        // TODO: only the first test works yet, change i<1 to i<10 for all tests
-        for(int i=0;i<1;i++) {
+        for(unsigned i=0;i<cx_nmemb(a);i++) {
             CX_TEST_ASSERT(cxJsonIsObject(a[i]));
             
             CxJsonValue *b = cxJsonClone(a[i], NULL);
@@ -1686,10 +1685,11 @@
             CX_TEST_ASSERT(b->type == a[i]->type);
             CX_TEST_ASSERT(cxJsonCompare(a[i], b) == 0);
             
-            // TODO: cxJsonToString(b, NULL) segfaults
-            //cxmutstr a_str = cxJsonToString(a[i], NULL);
-            //cxmutstr b_str = cxJsonToString(b, NULL);
-            //CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0);
+            cxmutstr a_str = cxJsonToString(a[i], NULL);
+            cxmutstr b_str = cxJsonToString(b, NULL);
+            CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0);
+            cx_strfree(&a_str);
+            cx_strfree(&b_str);
             
             cxJsonValueFree(b);
         }
@@ -1710,8 +1710,7 @@
     cxJsonFromString(NULL, "[ { \"array\": [ 1,2,3 ]}  ]", &a[5]);
     
     CX_TEST_DO {
-        // TODO: only the first 4 tests work. Change i<4 to i<6
-        for(int i=0;i<4;i++) {
+        for(unsigned i=0;i<cx_nmemb(a);i++) {
             CX_TEST_ASSERT(cxJsonIsArray(a[i]));
             
             CxJsonValue *b = cxJsonClone(a[i], NULL);
@@ -1719,11 +1718,12 @@
             CX_TEST_ASSERT(cxJsonIsArray(b));
             CX_TEST_ASSERT(cxJsonCompare(a[i], b) == 0);
             
-            // TODO: enable when cxJsonToString(b, NULL) works
-            //cxmutstr a_str = cxJsonToString(a[i], NULL);
-            //cxmutstr b_str = cxJsonToString(b, NULL);
-            //CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0);
-            
+            cxmutstr a_str = cxJsonToString(a[i], NULL);
+            cxmutstr b_str = cxJsonToString(b, NULL);
+            CX_TEST_ASSERT(cx_strcmp(a_str, b_str) == 0);
+            cx_strfree(&a_str);
+            cx_strfree(&b_str);
+
             cxJsonValueFree(b);
         }
     }

mercurial