aboutsummaryrefslogtreecommitdiff
path: root/util.h
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-02-20 12:06:28 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-02-20 12:06:28 +0100
commita445d8f1ad028f5ffcf17cf394574267e55c6e3c (patch)
tree664e79b8cdd2954bb2536aef15c2d5a8052d789f /util.h
parentc22d020db0cf1fe946847b890f5bfa70346e46d0 (diff)
downloadvis-a445d8f1ad028f5ffcf17cf394574267e55c6e3c.tar.gz
vis-a445d8f1ad028f5ffcf17cf394574267e55c6e3c.tar.xz
util: add overflow safe unsigned addition function
Diffstat (limited to 'util.h')
-rw-r--r--util.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/util.h b/util.h
index f2e3d65..a53deda 100644
--- 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