src/list.c

changeset 1633
fe24b68758bf
parent 1632
f74e4fc496a2
child 1635
4983b6a34996
--- a/src/list.c	Fri Dec 19 14:15:54 2025 +0100
+++ b/src/list.c	Fri Dec 19 17:24:18 2025 +0100
@@ -339,6 +339,13 @@
     return 0;
 }
 
+static int cx_list_ccmp_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);
+}
+
 void cx_list_init(
     struct cx_list_s *list,
     struct cx_list_class_s *cl,
@@ -352,8 +359,8 @@
     if (elem_size > 0) {
         list->collection.elem_size = elem_size;
         list->collection.simple_cmp = NULL;
-        list->collection.advanced_cmp = cx_ccmp_memcmp;
-        list->collection.cmp_data = &list->collection.elem_size;
+        list->collection.advanced_cmp = cx_list_ccmp_safe_memcmp;
+        list->collection.cmp_data = (void*)(uintptr_t)list->collection.elem_size;
         list->collection.store_pointer = false;
     } else {
         list->collection.elem_size = sizeof(void *);

mercurial