Skip to content

Commit a967966

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 70afa6f commit a967966

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

reftable/record.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,17 @@ static int reftable_obj_record_copy_from(void *rec, const void *src_rec,
504504
if (src->hash_prefix_len)
505505
memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);
506506

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+
}
512518

513519
return 0;
514520
}

0 commit comments

Comments
 (0)