vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit bca24c58de1548202f0e3603d4ac9f2940b9a6a5 parent a87ecf69b1575f397c730ebdef13f7203d4c48e0 Author: Marc André Tanner <mat@brain-dump.org> Date: Mon, 20 Feb 2017 12:06:28 +0100 util: add overflow safe unsigned addition function Diffstat:
| M | util.h | | | 14 | ++++++++++++++ |
1 file changed, 14 insertions(+), 0 deletions(-)
diff --git a/util.h b/util.h @@ -1,6 +1,9 @@ #ifndef UTIL_H #define UTIL_H +#include <stdint.h> +#include <stdbool.h> + #define LENGTH(x) ((int)(sizeof (x) / sizeof *(x))) #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MAX(a, b) ((a) < (b) ? (b) : (a)) @@ -9,4 +12,15 @@ #define ISUTF8(c) (((c)&0xC0)!=0x80) #define ISASCII(ch) ((unsigned char)ch < 0x80) +#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000 +#define addu __builtin_add_overflow +#else +static inline bool addu(size_t a, size_t b, size_t *c) { + if (SIZE_MAX - a < b) + return false; + *c = a + b; + return true; +} +#endif + #endif