]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-compat-util.h: introduce `u32_add()`
authorTaylor Blau <me@ttaylorr.com>
Sat, 6 Dec 2025 20:31:28 +0000 (15:31 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sat, 6 Dec 2025 22:38:08 +0000 (07:38 +0900)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h

index 398e0fac4fab6007903fdd9fd31dcc28fe531a65..a7aa5f05fc9445551041607feba87a738910ca72 100644 (file)
@@ -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))