Skip to content

Commit 35364d7

Browse files
ttaylorrgitster
authored andcommitted
git-compat-util.h: introduce u32_add()
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>
1 parent b40e7cb commit 35364d7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,14 @@ static inline int cast_size_t_to_int(size_t a)
670670
return (int)a;
671671
}
672672

673+
static inline uint32_t u32_add(uint32_t a, uint32_t b)
674+
{
675+
if (unsigned_add_overflows(a, b))
676+
die("uint32_t overflow: %"PRIuMAX" + %"PRIuMAX,
677+
(uintmax_t)a, (uintmax_t)b);
678+
return a + b;
679+
}
680+
673681
static inline uint64_t u64_mult(uint64_t a, uint64_t b)
674682
{
675683
if (unsigned_mult_overflows(a, b))

0 commit comments

Comments
 (0)