Re: [PATCH] do not depend on undefined signed integer overflow
- From
Jonathan Nieder <jrnieder@gmail.com>
- Date
- Oct 4, 2010, 20:48 UTC
- Message-ID
- <20101004204848.GI6466@burratino>
- In-Reply-To
- <1286221388-4908-1-git-send-email-kusmabite@gmail.com>
Erik Faye-Lund wrote:
Show 9 quoted lines
> --- a/git-compat-util.h > +++ b/git-compat-util.h > @@ -28,6 +28,15 @@ > #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) > #define bitsizeof(x) (CHAR_BIT * sizeof(x)) > > +/* > + * Signed integer overflow is undefined in C and some compilers take > + * advantage of this when optimizing
[...]
> + */ > +#define signed_add_overflows(a, b) \ > + ((b) > ((INTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a))) - (a)))
Clever. I like it.