Skip to content

Commit 48a25bb

Browse files
committed
Merge branch 'pw/midx-repack-overflow-fix'
Integer overflow fix around code paths for "git multi-pack-index repack".. * pw/midx-repack-overflow-fix: midx docs: clarify tie breaking midx: avoid negative array index midx repack: avoid potential integer overflow on 64 bit systems midx repack: avoid integer overflow on 32 bit systems
2 parents 277064b + 70b128c commit 48a25bb

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

Documentation/git-multi-pack-index.adoc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ write::
3838
+
3939
--
4040
--preferred-pack=<pack>::
41-
Optionally specify the tie-breaking pack used when
42-
multiple packs contain the same object. `<pack>` must
43-
contain at least one object. If not given, ties are
44-
broken in favor of the pack with the lowest mtime.
41+
When specified, break ties in favor of this pack when
42+
there are additional copies of its objects in other
43+
packs. Ties for objects not found in the preferred
44+
pack are always resolved in favor of the copy in the
45+
pack with the highest mtime. If unspecified, the pack
46+
with the lowest mtime is used by default. The
47+
preferred pack must have at least one object.
4548

4649
--[no-]bitmap::
4750
Control whether or not a multi-pack bitmap is written.

git-compat-util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,22 @@ static inline int cast_size_t_to_int(size_t a)
668668
return (int)a;
669669
}
670670

671+
static inline uint64_t u64_mult(uint64_t a, uint64_t b)
672+
{
673+
if (unsigned_mult_overflows(a, b))
674+
die("uint64_t overflow: %"PRIuMAX" * %"PRIuMAX,
675+
(uintmax_t)a, (uintmax_t)b);
676+
return a * b;
677+
}
678+
679+
static inline uint64_t u64_add(uint64_t a, uint64_t b)
680+
{
681+
if (unsigned_add_overflows(a, b))
682+
die("uint64_t overflow: %"PRIuMAX" + %"PRIuMAX,
683+
(uintmax_t)a, (uintmax_t)b);
684+
return a + b;
685+
}
686+
671687
/*
672688
* Limit size of IO chunks, because huge chunks only cause pain. OS X
673689
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in

midx-write.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15661566
_("Counting referenced objects"),
15671567
m->num_objects);
15681568
for (i = 0; i < m->num_objects; i++) {
1569-
int pack_int_id = nth_midxed_pack_int_id(m, i);
1569+
uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
15701570
count[pack_int_id]++;
15711571
display_progress(progress, i + 1);
15721572
}
@@ -1697,21 +1697,31 @@ static void fill_included_packs_batch(struct repository *r,
16971697

16981698
total_size = 0;
16991699
for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1700-
int pack_int_id = pack_info[i].pack_int_id;
1700+
uint32_t pack_int_id = pack_info[i].pack_int_id;
17011701
struct packed_git *p = m->packs[pack_int_id];
1702-
size_t expected_size;
1702+
uint64_t expected_size;
17031703

17041704
if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
17051705
continue;
17061706

1707-
expected_size = st_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;
17131719

1714-
total_size += expected_size;
1720+
if (unsigned_add_overflows(total_size, (size_t)expected_size))
1721+
total_size = SIZE_MAX;
1722+
else
1723+
total_size += expected_size;
1724+
17151725
include_pack[pack_int_id] = 1;
17161726
}
17171727

0 commit comments

Comments
 (0)