From b0ae227f34f3e3364f6bc81b69a800eb4dd6df05 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Sat, 2 May 2026 08:35:31 +0100 Subject: [PATCH] Don't try to malloc 0 bytes in xmerge For zero byte files, avoid allocation - this avoids undefined behavior around malloc(0) and potentially memcpy(..., NULL, 0). --- xmerge.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/xmerge.c b/xmerge.c index 6ebf73a..1b1903c 100644 --- a/xmerge.c +++ b/xmerge.c @@ -709,19 +709,23 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, goto out; if (!xscr1) { - result->ptr = xdl_malloc(mf2->size); - if (!result->ptr) - goto out; + if (mf2->size) { + result->ptr = xdl_malloc(mf2->size); + if (!result->ptr) + goto out; + memcpy(result->ptr, mf2->ptr, mf2->size); + result->size = mf2->size; + } status = 0; - memcpy(result->ptr, mf2->ptr, mf2->size); - result->size = mf2->size; } else if (!xscr2) { - result->ptr = xdl_malloc(mf1->size); - if (!result->ptr) - goto out; + if (mf1->size) { + result->ptr = xdl_malloc(mf1->size); + if (!result->ptr) + goto out; + memcpy(result->ptr, mf1->ptr, mf1->size); + result->size = mf1->size; + } status = 0; - memcpy(result->ptr, mf1->ptr, mf1->size); - result->size = mf1->size; } else { status = xdl_do_merge(&xe1, xscr1, &xe2, xscr2,