# HG changeset patch # User Mike Becker # Date 1765718967 -3600 # Node ID a1a728d7ebfd89df55049edb7a7ec06f6a10e20f # Parent aa55109f01d034e76d042ada82ef6b5a665bedf7 fix regression in cx_kvl_map_put() - after recent refactoring it returned the wrong value diff -r aa55109f01d0 -r a1a728d7ebfd src/json.c --- 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)); diff -r aa55109f01d0 -r a1a728d7ebfd src/kv_list.c --- 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) { diff -r aa55109f01d0 -r a1a728d7ebfd src/map.c --- 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) { diff -r aa55109f01d0 -r a1a728d7ebfd tests/test_json.c --- 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;itype == 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