Index: gpu/command_buffer/common/gles2_cmd_utils.h |
diff --git a/gpu/command_buffer/common/gles2_cmd_utils.h b/gpu/command_buffer/common/gles2_cmd_utils.h |
index 1661247d313a6521e429b4e8c05c1b52087c29fb..e6b8bf09aa2f8146307b945fb3511db2b6c2adb0 100644 |
--- a/gpu/command_buffer/common/gles2_cmd_utils.h |
+++ b/gpu/command_buffer/common/gles2_cmd_utils.h |
@@ -14,6 +14,7 @@ |
#include <string> |
#include <vector> |
+#include "base/numerics/safe_math.h" |
#include "gpu/command_buffer/common/gles2_utils_export.h" |
namespace gpu { |
@@ -25,44 +26,29 @@ namespace gles2 { |
// Multiplies 2 32 bit unsigned numbers checking for overflow. |
// If there was no overflow returns true. |
inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) { |
- if (b == 0) { |
- *dst = 0; |
- return true; |
- } |
- uint32_t v = a * b; |
- if (v / b != a) { |
- *dst = 0; |
- return false; |
- } |
- *dst = v; |
- return true; |
+ DCHECK(dst); |
+ base::CheckedNumeric<uint32_t> checked = a; |
+ checked *= b; |
+ *dst = checked.ValueOrDefault(0); |
+ return checked.IsValid(); |
} |
// Does an add checking for overflow. If there was no overflow returns true. |
inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) { |
- if (a + b < a) { |
- *dst = 0; |
- return false; |
- } |
- *dst = a + b; |
- return true; |
+ DCHECK(dst); |
+ base::CheckedNumeric<uint32_t> checked = a; |
+ checked += b; |
+ *dst = checked.ValueOrDefault(0); |
+ return checked.IsValid(); |
} |
// Does an add checking for overflow. If there was no overflow returns true. |
inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) { |
- int64_t sum64 = static_cast<int64_t>(a) + b; |
- int32_t sum32 = static_cast<int32_t>(sum64); |
- bool safe = sum64 == static_cast<int64_t>(sum32); |
- *dst = safe ? sum32 : 0; |
- return safe; |
-} |
- |
-// Return false if |value| is more than a 32 bit integer can represent. |
-template<typename T> |
-inline bool FitInt32NonNegative(T value) { |
- const int32_t max = std::numeric_limits<int32_t>::max(); |
- return (std::numeric_limits<T>::max() <= max || |
- value <= static_cast<T>(max)); |
+ DCHECK(dst); |
+ base::CheckedNumeric<int32_t> checked = a; |
+ checked += b; |
+ *dst = checked.ValueOrDefault(0); |
+ return checked.IsValid(); |
} |
// Utilties for GLES2 support. |