xdiff: don't try to malloc 0 bytes

For zero byte files, avoid allocation - this avoids undefined behavior
around malloc(0) and potentially memcpy(..., NULL, 0).
This commit is contained in:
Edward Thomson
2026-05-02 08:16:15 +01:00
parent 047da6a2ca
commit 1803dfa284

24
deps/xdiff/xmerge.c vendored
View File

@@ -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,