kv-list: fix a possible source of UB when allocation fails during list creation

Thu, 11 Sep 2025 20:10:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Sep 2025 20:10:12 +0200
changeset 1371
37dcaeebe5ca
parent 1370
607f822c79fe
child 1372
9c176073e771

kv-list: fix a possible source of UB when allocation fails during list creation

relates to #461

src/kv_list.c file | annotate | diff | comparison | revisions
--- a/src/kv_list.c	Thu Sep 11 20:09:33 2025 +0200
+++ b/src/kv_list.c	Thu Sep 11 20:10:12 2025 +0200
@@ -357,9 +357,6 @@
     // reallocate the list to add memory for storing the metadata
     cx_kv_list *kv_list = cxRealloc(allocator, list, sizeof(struct cx_kv_list_s));
 
-    // zero the custom destructor information
-    memset((char*)kv_list + offsetof(cx_kv_list, list_destr), 0, sizeof(void*)*6);
-
     // if any of the reallocations failed, we bail out
     if (kv_map != NULL && kv_list != NULL) {
         map = (CxMap*) kv_map;
@@ -370,6 +367,9 @@
         return NULL;
     } // LCOV_EXCL_STOP
 
+    // zero the custom destructor information
+    memset((char*)kv_list + offsetof(cx_kv_list, list_destr), 0, sizeof(void*)*6);
+
     // combine the list and the map aspect
     kv_list->map = kv_map;
     kv_map->list = kv_list;

mercurial