pack-bitmap: fix memory leak if `load_bitmap_entries_v1` failed

In pack-bitmap.c:load_bitmap_entries_v1, the function `read_bitmap_1` allocates a bitmap and reads index data into it. However, if any of the validation checks following the allocation fail, the allocated bitmap is not freed, resulting in a memory leak. To avoid this, the validation checks should be performed before the bitmap is allocated. Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lidong Yan committed May 12, 2025 at 12:22 UTC 7291c2be6a276f8b3693fb7d8dd763cee14c2485
1 file changed +4 -4
pack-bitmap.c
+4 -4
@@ -388,10 +388,6 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
388 return error(_("corrupt ewah bitmap: commit index %u out of range"),
389 (unsigned)commit_idx_pos);
390
391 - bitmap = read_bitmap_1(index);
392 - if (!bitmap)
393 - return -1;
394 -
391 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
392 return error(_("corrupted bitmap pack index"));
393
@@ -402,6 +398,10 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
398 return error(_("invalid XOR offset in bitmap pack index"));
399 }
400
401 + bitmap = read_bitmap_1(index);
402 + if (!bitmap)
403 + return -1;
404 +
405 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
406 index, bitmap, &oid, xor_bitmap, flags);
407 }