aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/assert.h
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2019-01-10 08:56:38 +0100
committerGitHub <noreply@github.com>2019-01-10 08:56:38 +0100
commit6d8b5989bc84c0b54cb6804af0851cb3322234b2 (patch)
tree60e0d4b164ae981c1ed71a333c3fd0df8bee8e54 /src/nvim/assert.h
parent57c7e1d4a0d7285d9de5b9035e91f546654268da (diff)
parentfc4ca5bdd8c5a2b37b6efe34a9b32a1bd75c57af (diff)
downloadrneovim-6d8b5989bc84c0b54cb6804af0851cb3322234b2.tar.gz
rneovim-6d8b5989bc84c0b54cb6804af0851cb3322234b2.tar.bz2
rneovim-6d8b5989bc84c0b54cb6804af0851cb3322234b2.zip
Merge #9472 from justinmk/pvs-warnings2
Diffstat (limited to 'src/nvim/assert.h')
-rw-r--r--src/nvim/assert.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/nvim/assert.h b/src/nvim/assert.h
index 761636305e..29195a49dc 100644
--- a/src/nvim/assert.h
+++ b/src/nvim/assert.h
@@ -121,4 +121,34 @@
((enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }) 0)
#endif
+/// @def STRICT_ADD
+/// @brief Adds (a + b) and stores result in `c`. Aborts on overflow.
+///
+/// Requires GCC 5+ and Clang 3.8+
+/// https://clang.llvm.org/docs/LanguageExtensions.html
+/// https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
+///
+/// Alternative for compilers without __builtin_xx_overflow ?
+/// https://stackoverflow.com/a/44830670/152142
+///
+/// @param MAX Maximum value of the narrowest type of operand.
+/// Not used if compiler supports __builtin_add_overflow.
+#if HAVE_BUILTIN_ADD_OVERFLOW
+# define STRICT_ADD(a, b, c, t) \
+ do { if (__builtin_add_overflow(a, b, c)) { abort(); } } while (0)
+#else
+# define STRICT_ADD(a, b, c, t) \
+ do { *(c) = (t)(a + b); } while (0)
+#endif
+
+/// @def STRICT_SUB
+/// @brief Subtracts (a - b) and stores result in `c`. Aborts on overflow.
+#if HAVE_BUILTIN_ADD_OVERFLOW
+# define STRICT_SUB(a, b, c, t) \
+ do { if (__builtin_sub_overflow(a, b, c)) { abort(); } } while (0)
+#else
+# define STRICT_SUB(a, b, c, t) \
+ do { *(c) = (t)(a - b); } while (0)
+#endif
+
#endif // NVIM_ASSERT_H