summaryrefslogtreecommitdiff
path: root/goo
diff options
context:
space:
mode:
authorLE GARREC Vincent <gitlab-freedesktop@le-garrec.fr>2019-03-31 20:49:25 +0000
committerAlbert Astals Cid <tsdgeos@yahoo.es>2019-03-31 20:49:25 +0000
commitea12ae67be12883e4626b34db8f2f849fb6d56d7 (patch)
tree7aa292157016a6e1094441ed95c57c0adb0b57f8 /goo
parent516e6149572a16747f21d6510f844b5879954ec0 (diff)
ofz-8443: Integer-overflow in identifyCFF
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8443
Diffstat (limited to 'goo')
-rw-r--r--goo/GooCheckedOps.h26
1 files changed, 15 insertions, 11 deletions
diff --git a/goo/GooCheckedOps.h b/goo/GooCheckedOps.h
index 5eedf34b..c49a621f 100644
--- a/goo/GooCheckedOps.h
+++ b/goo/GooCheckedOps.h
@@ -12,17 +12,21 @@
#ifndef GOO_CHECKED_OPS_H
#define GOO_CHECKED_OPS_H
-#include <climits>
#include <limits>
+#include <type_traits>
-inline bool checkedAssign(long long lz, int *z) {
- static_assert(LLONG_MAX > INT_MAX, "Need type larger than int to perform overflow checks.");
+template<typename T> inline bool checkedAssign(long long lz, T *z) {
+ static_assert(std::numeric_limits<long long>::max() > std::numeric_limits<T>::max(),
+ "The max of long long type must be larger to perform overflow checks.");
+ static_assert(std::numeric_limits<long long>::min() < std::numeric_limits<T>::min(),
+ "The min of long long type must be smaller to perform overflow checks.");
- if (lz > INT_MAX || lz < INT_MIN) {
+ if (lz > std::numeric_limits<T>::max() ||
+ lz < std::numeric_limits<T>::min()) {
return true;
}
- *z = static_cast<int>(lz);
+ *z = static_cast<T>(lz);
return false;
}
@@ -30,18 +34,18 @@ inline bool checkedAssign(long long lz, int *z) {
#define __has_builtin(x) 0
#endif
-inline bool checkedAdd(int x, int y, int *z) {
-#if __GNUC__ >= 5 || __has_builtin(__builtin_sadd_overflow)
- return __builtin_sadd_overflow(x, y, z);
+template<typename T> inline bool checkedAdd(T x, T y, T *z) {
+#if __GNUC__ >= 5 || __has_builtin(__builtin_add_overflow)
+ return __builtin_add_overflow(x, y, z);
#else
const auto lz = static_cast<long long>(x) + static_cast<long long>(y);
return checkedAssign(lz, z);
#endif
}
-inline bool checkedMultiply(int x, int y, int *z) {
-#if __GNUC__ >= 5 || __has_builtin(__builtin_smul_overflow)
- return __builtin_smul_overflow(x, y, z);
+template<typename T> inline bool checkedMultiply(T x, T y, T *z) {
+#if __GNUC__ >= 5 || __has_builtin(__builtin_mul_overflow)
+ return __builtin_mul_overflow(x, y, z);
#else
const auto lz = static_cast<long long>(x) * static_cast<long long>(y);
return checkedAssign(lz, z);