From 38ef83d846c25a24fe01655b1f2b5ca91c94f997 Mon Sep 17 00:00:00 2001 From: Jonathan Dieter Date: Tue, 4 Apr 2023 21:14:49 +0100 Subject: [PATCH] Ensure memory is freed when zrealloc is called with size 0 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 --- src/lib/zck.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib/zck.c b/src/lib/zck.c index 8593281..0f5fc52 100644 --- a/src/lib/zck.c +++ b/src/lib/zck.c @@ -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. -- 2.30.2