Commit 8b884b9a9e3039be14caea7b130b1f48f73de01d

Edward Thomson 2021-06-24T10:15:52

util: include git__add_int64_overflow Cherry-pick `git__add_int64_overflow` functionality introduced in e99e833f4808e027de8e984b9b99544ca41e28e2.

diff --git a/src/integer.h b/src/integer.h
index 067c0be..a7865c3 100644
--- a/src/integer.h
+++ b/src/integer.h
@@ -77,6 +77,9 @@ GIT_INLINE(int) git__is_int(long long p)
 # define git__sub_int_overflow(out, one, two) \
     __builtin_ssub_overflow(one, two, out)
 
+# define git__add_int64_overflow(out, one, two) \
+    __builtin_add_overflow(one, two, out)
+
 /* Use Microsoft's safe integer handling functions where available */
 #elif defined(_MSC_VER)
 
@@ -92,6 +95,9 @@ GIT_INLINE(int) git__is_int(long long p)
 #define git__sub_int_overflow(out, one, two) \
     (IntSub(one, two, out) != S_OK)
 
+#define git__add_int64_overflow(out, one, two) \
+    (LongLongAdd(one, two, out) != S_OK)
+
 #else
 
 /**
@@ -136,6 +142,15 @@ GIT_INLINE(bool) git__sub_int_overflow(int *out, int one, int two)
 	return false;
 }
 
+GIT_INLINE(bool) git__add_int64_overflow(int64_t *out, int64_t one, int64_t two)
+{
+	if ((two > 0 && one > (INT64_MAX - two)) ||
+	    (two < 0 && one < (INT64_MIN - two)))
+		return true;
+	*out = one + two;
+	return false;
+}
+
 #endif
 
 #endif