Skip to content

Commit 2a15964

Browse files
ttaylorrgitster
authored andcommitted
builtin/repack.c: be more conservative with unsigned overflows
There are a number of places in the geometric repack code where we multiply the number of objects in a pack by another unsigned value. We trust that the number of objects in a pack is always representable by a uint32_t, but we don't necessarily trust that that number can be multiplied without overflow. Sprinkle some unsigned_add_overflows() and unsigned_mult_overflows() in split_pack_geometry() to check that we never overflow any unsigned types when adding or multiplying them. Arguably these checks are a little too conservative, and certainly they do not help the readability of this function. But they are serving a useful purpose, so I think they are worthwhile overall. Suggested-by: Junio C Hamano <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 13d746a commit 2a15964

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

builtin/repack.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ static void split_pack_geometry(struct pack_geometry *geometry, int factor)
363363
for (i = geometry->pack_nr - 1; i > 0; i--) {
364364
struct packed_git *ours = geometry->pack[i];
365365
struct packed_git *prev = geometry->pack[i - 1];
366+
367+
if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
368+
die(_("pack %s too large to consider in geometric "
369+
"progression"),
370+
prev->pack_name);
371+
366372
if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
367373
break;
368374
}
@@ -388,11 +394,25 @@ static void split_pack_geometry(struct pack_geometry *geometry, int factor)
388394
* packs in the heavy half need to be joined into it (if any) to restore
389395
* the geometric progression.
390396
*/
391-
for (i = 0; i < split; i++)
392-
total_size += geometry_pack_weight(geometry->pack[i]);
397+
for (i = 0; i < split; i++) {
398+
struct packed_git *p = geometry->pack[i];
399+
400+
if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
401+
die(_("pack %s too large to roll up"), p->pack_name);
402+
total_size += geometry_pack_weight(p);
403+
}
393404
for (i = split; i < geometry->pack_nr; i++) {
394405
struct packed_git *ours = geometry->pack[i];
406+
407+
if (unsigned_mult_overflows(factor, total_size))
408+
die(_("pack %s too large to roll up"), ours->pack_name);
409+
395410
if (geometry_pack_weight(ours) < factor * total_size) {
411+
if (unsigned_add_overflows(total_size,
412+
geometry_pack_weight(ours)))
413+
die(_("pack %s too large to roll up"),
414+
ours->pack_name);
415+
396416
split++;
397417
total_size += geometry_pack_weight(ours);
398418
} else

0 commit comments

Comments
 (0)