Skip to content

Commit 7dc2c54

Browse files
committed
git-compat-util.h: drop u64_add(), u64_mult() helpers
The u64_add() and u64_mult() helper functions were introduced in b103881 (midx repack: avoid integer overflow on 32 bit systems, 2025-05-22) to implement overflow checks during a fixed-point calculation when estimating pack sizes in the MIDX writing code. However, those functions call die() when either the addition or multiplication of their operands (depending on which function is being called) would cause an overflow. This does not allow the caller to provide a more detailed message, presenting the user with an opaque message like: fatal: uint64_t overflow: M * N Let's discourage these opaque error messages by dropping these functions entirely and instead having the caller use unsigned_mult_overflows() or unsigned_add_overflows() themselves, providing the caller the opportunity to come up with their own die() message. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>
1 parent 8745eae commit 7dc2c54

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

git-compat-util.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -641,22 +641,6 @@ static inline int cast_size_t_to_int(size_t a)
641641
return (int)a;
642642
}
643643

644-
static inline uint64_t u64_mult(uint64_t a, uint64_t b)
645-
{
646-
if (unsigned_mult_overflows(a, b))
647-
die("uint64_t overflow: %"PRIuMAX" * %"PRIuMAX,
648-
(uintmax_t)a, (uintmax_t)b);
649-
return a * b;
650-
}
651-
652-
static inline uint64_t u64_add(uint64_t a, uint64_t b)
653-
{
654-
if (unsigned_add_overflows(a, b))
655-
die("uint64_t overflow: %"PRIuMAX" + %"PRIuMAX,
656-
(uintmax_t)a, (uintmax_t)b);
657-
return a + b;
658-
}
659-
660644
/*
661645
* Limit size of IO chunks, because huge chunks only cause pain. OS X
662646
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in

midx-write.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,8 +1738,18 @@ static void fill_included_packs_batch(struct repository *r,
17381738
*/
17391739
expected_size = (uint64_t)pack_info[i].referenced_objects << 14;
17401740
expected_size /= p->num_objects;
1741-
expected_size = u64_mult(expected_size, p->pack_size);
1742-
expected_size = u64_add(expected_size, 1u << 13) >> 14;
1741+
1742+
if (unsigned_mult_overflows(expected_size, p->pack_size))
1743+
die(_("overflow during fixed-point multiply (%"PRIu64" "
1744+
"* %"PRIu64")"),
1745+
expected_size, (uint64_t)p->pack_size);
1746+
expected_size = expected_size * p->pack_size;
1747+
1748+
if (unsigned_add_overflows(expected_size, 1u << 13))
1749+
die(_("overflow during fixed-point rounding (%"PRIu64" "
1750+
" + %"PRIu64")"),
1751+
expected_size, (uint64_t)(1ul << 13));
1752+
expected_size = (expected_size + (1u << 13)) >> 14;
17431753

17441754
if (expected_size >= batch_size)
17451755
continue;

0 commit comments

Comments
 (0)