X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=git-compat-util.h;h=2af8d3edbe35dce35e940c5cccb91f9e06ebeff8;hb=9018af4f98b98738f3cd1268a16d143762ebb6f2;hp=81883e7270daadafb4d827e894fa4645ff2e2b66;hpb=0c61c6de3497900616b66a65dc41b27544131ed5;p=git.git diff --git a/git-compat-util.h b/git-compat-util.h index 81883e727..2af8d3edb 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -28,6 +28,18 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) #define bitsizeof(x) (CHAR_BIT * sizeof(x)) +#define maximum_signed_value_of_type(a) \ + (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a))) + +/* + * Signed integer overflow is undefined in C, so here's a helper macro + * to detect if the sum of two integers will overflow. + * + * Requires: a >= 0, typeof(a) equals typeof(b) + */ +#define signed_add_overflows(a, b) \ + ((b) > maximum_signed_value_of_type(a) - (a)) + #ifdef __GNUC__ #define TYPEOF(x) (__typeof__(x)) #else