reftable/record: stop using `COPY_ARRAY()`

Drop our use of `COPY_ARRAY()`, replacing it with an open-coded variant thereof. This is done to reduce our dependency on the Git library. While at it, guard the whole array copy logic so that we only copy it in case there actually is anything to be copied. Otherwise, we may end up trying to allocate a zero-sized array, which will return a NULL pointer and thus cause us to return an `REFTABLE_OUT_OF_MEMORY_ERROR`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 18, 2025 at 10:20 UTC a967966432f25324c79524c0cb18d6e152d0b6af
1 file changed +11 -5
reftable/record.c
+11 -5
@@ -504,11 +504,17 @@ static int reftable_obj_record_copy_from(void *rec, const void *src_rec,
504 if (src->hash_prefix_len)
505 memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);
506
507 - REFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len);
508 - if (!obj->offsets)
509 - return REFTABLE_OUT_OF_MEMORY_ERROR;
510 - obj->offset_len = src->offset_len;
511 - COPY_ARRAY(obj->offsets, src->offsets, src->offset_len);
507 + if (src->offset_len) {
508 + if (sizeof(*src->offsets) > SIZE_MAX / src->offset_len)
509 + return REFTABLE_OUT_OF_MEMORY_ERROR;
510 +
511 + REFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len);
512 + if (!obj->offsets)
513 + return REFTABLE_OUT_OF_MEMORY_ERROR;
514 +
515 + memcpy(obj->offsets, src->offsets, sizeof(*src->offsets) * src->offset_len);
516 + obj->offset_len = src->offset_len;
517 + }
518
519 return 0;
520 }