fix that certain reallocate functions did not properly support size zero

Fri, 05 Dec 2025 16:36:10 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Dec 2025 16:36:10 +0100
changeset 1541
d06aa9db0408
parent 1540
411cf6fd268f
child 1542
197450c2b0b3

fix that certain reallocate functions did not properly support size zero

CHANGELOG file | annotate | diff | comparison | revisions
docs/Writerside/topics/about.md file | annotate | diff | comparison | revisions
src/allocator.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Fri Dec 05 16:22:57 2025 +0100
+++ b/CHANGELOG	Fri Dec 05 16:36:10 2025 +0100
@@ -1,7 +1,9 @@
 Version 4.0 - tbd
 ------------------------
 
-* adds cx_system_page_size() to allocator.h
+ * adds cx_system_page_size() to allocator.h
+ * fix that cxReallocate(), cxReallocateArray(), cx_reallocate(), and cx_reallocatearray()
+   were not returning zero after freeing the memory when passed a size of zero
 
 Version 3.2 - 2025-11-30
 ------------------------
--- a/docs/Writerside/topics/about.md	Fri Dec 05 16:22:57 2025 +0100
+++ b/docs/Writerside/topics/about.md	Fri Dec 05 16:36:10 2025 +0100
@@ -29,6 +29,8 @@
 ### Version 4.0 - preview {collapsible="true"}
 
 * adds cx_system_page_size() to allocator.h
+* fix that cxReallocate(), cxReallocateArray(), cx_reallocate(), and cx_reallocatearray()
+  were not returning zero after freeing the memory when passed a size of zero
 
 ### Version 3.2 - 2025-11-30 {collapsible="true"}
 
--- a/src/allocator.c	Fri Dec 05 16:22:57 2025 +0100
+++ b/src/allocator.c	Fri Dec 05 16:36:10 2025 +0100
@@ -108,6 +108,11 @@
         void **mem,
         size_t n
 ) {
+    if (n == 0) {
+        free(*mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = realloc(*mem, n);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE
@@ -122,6 +127,11 @@
         size_t nmemb,
         size_t size
 ) {
+    if (nmemb == 0 || size == 0) {
+        free(*mem);
+        *mem = NULL;
+        return 0;
+    }
     size_t n;
     if (cx_szmul(nmemb, size, &n)) {
         errno = EOVERFLOW;
@@ -185,6 +195,11 @@
         void **mem,
         size_t n
 ) {
+    if (n == 0) {
+        cxFree(allocator, *mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE
@@ -200,6 +215,11 @@
         size_t nmemb,
         size_t size
 ) {
+    if (nmemb == 0 || size == 0) {
+        cxFree(allocator, *mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE

mercurial