[PATCH 10/17] git-compat-util.h: introduce `u32_add()`
- From
Taylor Blau <me@ttaylorr.com>
- Date
- Dec 6, 2025, 20:31 UTC
- Message-ID
- <776eb88905f53dd929e5b7cb23593976a2f1aad3.1765053054.git.me@ttaylorr.com>
- In-Reply-To
- <cover.1765053054.git.me@ttaylorr.com>
A future commit will want to add two 32-bit unsigned values together while checking for overflow. Introduce a variant of the u64_add() function for operating on 32-bit inputs.
Signed-off-by: Taylor Blau <me@ttaylorr.com> --- git-compat-util.h | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h index 398e0fac4fa..a7aa5f05fc9 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -670,6 +670,14 @@ static inline int cast_size_t_to_int(size_t a) return (int)a; } +static inline uint32_t u32_add(uint32_t a, uint32_t b) +{ + if (unsigned_add_overflows(a, b)) + die("uint32_t overflow: %"PRIuMAX" + %"PRIuMAX, + (uintmax_t)a, (uintmax_t)b); + return a + b; +} + static inline uint64_t u64_mult(uint64_t a, uint64_t b) { if (unsigned_mult_overflows(a, b))
-- 2.52.0.171.gd6a4e6b6955