ucx/hash_map.c

branch
dav-2
changeset 894
e86049631677
parent 891
4d58cbcc9efa
--- a/ucx/hash_map.c	Tue Dec 30 21:39:38 2025 +0100
+++ b/ucx/hash_map.c	Wed Dec 31 16:41:16 2025 +0100
@@ -400,6 +400,13 @@
         cx_hash_map_iterator,
 };
 
+static int cx_map_cmpfunc2_safe_memcmp(const void *a, const void *b, void *c) {
+    // it is not safe to store a pointer to the size in the list
+    // because the entire list structure might get reallocated
+    size_t elem_size = (size_t)(uintptr_t)c;
+    return memcmp(a, b, elem_size);
+}
+
 CxMap *cxHashMapCreate(
         const CxAllocator *allocator,
         size_t itemsize,
@@ -432,8 +439,8 @@
 
     if (itemsize > 0) {
         map->base.collection.elem_size = itemsize;
-        map->base.collection.advanced_cmp = cx_ccmp_memcmp;
-        map->base.collection.cmp_data = &map->base.collection.elem_size;
+        map->base.collection.advanced_cmp = cx_map_cmpfunc2_safe_memcmp;
+        map->base.collection.cmp_data = (void*)(uintptr_t)itemsize;
     } else {
         map->base.collection.elem_size = sizeof(void *);
         map->base.collection.store_pointer = true;

mercurial