aboutsummaryrefslogtreecommitdiffstats
path: root/src/3rdparty/masm/wtf/CheckedArithmetic.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/3rdparty/masm/wtf/CheckedArithmetic.h')
-rw-r--r--src/3rdparty/masm/wtf/CheckedArithmetic.h33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/3rdparty/masm/wtf/CheckedArithmetic.h b/src/3rdparty/masm/wtf/CheckedArithmetic.h
index f5d3b75500..dd4acbb9b5 100644
--- a/src/3rdparty/masm/wtf/CheckedArithmetic.h
+++ b/src/3rdparty/masm/wtf/CheckedArithmetic.h
@@ -27,6 +27,7 @@
#define CheckedArithmetic_h
#include <wtf/Assertions.h>
+#include <wtf/EnumClass.h>
#include <wtf/TypeTraits.h>
#include <limits>
@@ -66,9 +67,15 @@
namespace WTF {
+ENUM_CLASS(CheckedState)
+{
+ DidOverflow,
+ DidNotOverflow
+} ENUM_CLASS_END(CheckedState);
+
class CrashOnOverflow {
-protected:
- NO_RETURN_DUE_TO_CRASH void overflowed()
+public:
+ static NO_RETURN_DUE_TO_CRASH void overflowed()
{
CRASH();
}
@@ -314,10 +321,13 @@ template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOper
static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
{
- ResultType temp = lhs * rhs;
- if (temp < lhs)
+ if (!lhs || !rhs) {
+ result = 0;
+ return true;
+ }
+ if (std::numeric_limits<ResultType>::max() / lhs < rhs)
return false;
- result = temp;
+ result = lhs * rhs;
return true;
}
@@ -534,10 +544,12 @@ public:
return m_value;
}
- bool safeGet(T& value) const WARN_UNUSED_RETURN
+ inline CheckedState safeGet(T& value) const WARN_UNUSED_RETURN
{
value = m_value;
- return this->hasOverflowed();
+ if (this->hasOverflowed())
+ return CheckedState::DidOverflow;
+ return CheckedState::DidNotOverflow;
}
// Mutating assignment
@@ -638,7 +650,7 @@ template <typename U, typename V, typename OverflowHandler> static inline Checke
{
U x = 0;
V y = 0;
- bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
+ bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet(y) == CheckedState::DidOverflow;
typename Result<U, V>::ResultType result = 0;
overflowed |= !safeAdd(x, y, result);
if (overflowed)
@@ -650,7 +662,7 @@ template <typename U, typename V, typename OverflowHandler> static inline Checke
{
U x = 0;
V y = 0;
- bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
+ bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet(y) == CheckedState::DidOverflow;
typename Result<U, V>::ResultType result = 0;
overflowed |= !safeSub(x, y, result);
if (overflowed)
@@ -662,7 +674,7 @@ template <typename U, typename V, typename OverflowHandler> static inline Checke
{
U x = 0;
V y = 0;
- bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
+ bool overflowed = lhs.safeGet(x) == CheckedState::DidOverflow || rhs.safeGet(y) == CheckedState::DidOverflow;
typename Result<U, V>::ResultType result = 0;
overflowed |= !safeMultiply(x, y, result);
if (overflowed)
@@ -703,6 +715,7 @@ template <typename U, typename V, typename OverflowHandler> static inline Checke
}
using WTF::Checked;
+using WTF::CheckedState;
using WTF::RecordOverflow;
#endif