Ensure memory is freed when zrealloc is called with size 0
authorJonathan Dieter <jdieter@gmail.com>
Tue, 4 Apr 2023 20:14:49 +0000 (21:14 +0100)
committerJonathan Dieter <jdieter@gmail.com>
Tue, 4 Apr 2023 20:14:49 +0000 (21:14 +0100)
zrealloc is supposed to handle freeing the old pointer when called, but
when called with size 0, it was not always working as expected.  This
commit fixes the bug.  Thanks to Agostino Sarubbo of Gentoo for providing
a bug report with a reproducible test case.

Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
src/lib/zck.c

index 859328166d64d3885d9c06809f72eae124d9aa82..0f5fc528bfae32b8658c423812d6a2a6fdb44331 100644 (file)
@@ -134,6 +134,13 @@ void *zmalloc(size_t size) {
 }
 
 void *zrealloc(void *ptr, size_t size) {
+    /* Handle requested size of zero */
+    if(size == 0) {
+        if(ptr != NULL)
+            free(ptr);
+        return NULL;
+    }
+
     void *ret = realloc(ptr, size);
     /*
      * Realloc does not touch the original block if fails.