Skip to content

Commit f874c0e

Browse files
phillipwoodgitster
authored andcommitted
midx repack: avoid potential integer overflow on 64 bit systems
On a 64 bit system the calculation p->pack_size * pack_info[i].referenced_objects could overflow. If a pack file contains 2^28 objects with an average compressed size of 1KB then the pack size will be 2^38B. If all of the objects are referenced by the multi-pack index the sum above will overflow. Avoid this by using shifted integer arithmetic and changing the order of the calculation so that the pack size is divided by the total number of objects in the pack before multiplying by the number of objects referenced by the multi-pack index. Using a shift of 14 bits should give reasonable accuracy while avoiding overflow for pack sizes less that 1PB. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b103881 commit f874c0e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

midx-write.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,9 +1704,15 @@ static void fill_included_packs_batch(struct repository *r,
17041704
if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
17051705
continue;
17061706

1707-
expected_size = uint64_mult(p->pack_size,
1708-
pack_info[i].referenced_objects);
1707+
/*
1708+
* Use shifted integer arithmetic to calculate the
1709+
* expected pack size to ~4 significant digits without
1710+
* overflow for packsizes less that 1PB.
1711+
*/
1712+
expected_size = (uint64_t)pack_info[i].referenced_objects << 14;
17091713
expected_size /= p->num_objects;
1714+
expected_size = u64_mult(expected_size, p->pack_size);
1715+
expected_size = u64_add(expected_size, 1u << 13) >> 14;
17101716

17111717
if (expected_size >= batch_size)
17121718
continue;

0 commit comments

Comments
 (0)