@@ -77,6 +77,11 @@ GIT_INLINE(int) git__is_int(long long p)
7777# define git__sub_int_overflow (out , one , two ) \
7878 __builtin_ssub_overflow(one, two, out)
7979
80+ # define git__add_int64_overflow (out , one , two ) \
81+ __builtin_add_overflow(one, two, out)
82+ # define git__multiply_int64_overflow (out , one , two ) \
83+ __builtin_mul_overflow(one, two, out)
84+
8085/* Use Microsoft's safe integer handling functions where available */
8186#elif defined(_MSC_VER )
8287
@@ -87,11 +92,17 @@ GIT_INLINE(int) git__is_int(long long p)
8792 (SizeTAdd(one, two, out) != S_OK)
8893# define git__multiply_sizet_overflow (out , one , two ) \
8994 (SizeTMult(one, two, out) != S_OK)
95+
9096#define git__add_int_overflow (out , one , two ) \
9197 (IntAdd(one, two, out) != S_OK)
9298#define git__sub_int_overflow (out , one , two ) \
9399 (IntSub(one, two, out) != S_OK)
94100
101+ #define git__add_int64_overflow (out , one , two ) \
102+ (LongLongAdd(one, two, out) != S_OK)
103+ #define git__multiply_int64_overflow (out , one , two ) \
104+ (LongLongMult(one, two, out) != S_OK)
105+
95106#else
96107
97108/**
@@ -136,6 +147,26 @@ GIT_INLINE(bool) git__sub_int_overflow(int *out, int one, int two)
136147 return false;
137148}
138149
150+ GIT_INLINE (bool ) git__add_int64_overflow (int64_t * out , int64_t one , int64_t two )
151+ {
152+ if ((two > 0 && one > (INT64_MAX - two )) ||
153+ (two < 0 && one < (INT64_MIN - two )))
154+ return true;
155+ * out = one + two ;
156+ return false;
157+ }
158+
159+ GIT_INLINE (bool ) git__multiply_int64_overflow (int64_t * out , int64_t one , int64_t two )
160+ {
161+ if ((one == -1 && two == INT_MIN ) ||
162+ (two == -1 && one == INT_MIN ) ||
163+ (one && INT64_MAX / one < two ) ||
164+ (one && INT64_MIN / one > two ))
165+ return true;
166+ * out = one * two ;
167+ return false;
168+ }
169+
139170#endif
140171
141172#endif
0 commit comments