diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c --- a/mercurial/mpatch.c +++ b/mercurial/mpatch.c @@ -20,6 +20,7 @@ of the GNU General Public License, incorporated herein by reference. */ +#include #include #include @@ -27,6 +28,15 @@ #include "compat.h" #include "mpatch.h" +/* VC9 doesn't include bool and lacks stdbool.h based on cext/util.h */ +#if defined(_MSC_VER) || __STDC_VERSION__ < 199901L +#define true 1 +#define false 0 +typedef unsigned char bool; +#else +#include +#endif + static struct mpatch_flist *lalloc(ssize_t size) { struct mpatch_flist *a = NULL; @@ -60,6 +70,24 @@ static ssize_t lsize(struct mpatch_flist return a->tail - a->head; } +/* add helper to add src and *dest iff it won't overflow */ +static inline bool safeadd(int src, int *dest) +{ + if ((src > 0) == (*dest > 0)) { + if (*dest > 0) { + if (src > (INT_MAX - *dest)) { + return false; + } + } else { + if (src < (INT_MIN - *dest)) { + return false; + } + } + } + *dest += src; + return true; +} + /* move hunks in source that are less cut to dest, compensating for changes in offset. the last hunk may be split if necessary. */