Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(752)

Unified Diff: base/numerics/safe_math_impl.h

Issue 2529413002: Loosen restrictions on CheckedNumeric bitwise operators (Closed)
Patch Set: more tests Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/numerics/safe_math.h ('k') | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numerics/safe_math_impl.h
diff --git a/base/numerics/safe_math_impl.h b/base/numerics/safe_math_impl.h
index 68ad22af5a572c6a380597177b7104ce202a8842..15de619e76fae11ce35d8ecac5b950c4e7ba432b 100644
--- a/base/numerics/safe_math_impl.h
+++ b/base/numerics/safe_math_impl.h
@@ -602,19 +602,17 @@ struct CheckedRshOp<
template <typename T, typename U, class Enable = void>
struct CheckedAndOp {};
-// For simplicity we support only unsigned integers.
+// For simplicity we support only unsigned integer results.
template <typename T, typename U>
struct CheckedAndOp<T,
U,
typename std::enable_if<std::is_integral<T>::value &&
- std::is_integral<U>::value &&
- !std::is_signed<T>::value &&
- !std::is_signed<U>::value>::type> {
- using result_type =
- typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type;
+ std::is_integral<U>::value>::type> {
+ using result_type = typename UnsignedIntegerForSize<
+ typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type>::type;
template <typename V = result_type>
static bool Do(T x, U y, V* result) {
- result_type tmp = x & y;
+ result_type tmp = static_cast<result_type>(x) & static_cast<result_type>(y);
*result = static_cast<V>(tmp);
return IsValueInRangeForNumericType<V>(tmp);
}
@@ -628,14 +626,12 @@ template <typename T, typename U>
struct CheckedOrOp<T,
U,
typename std::enable_if<std::is_integral<T>::value &&
- std::is_integral<U>::value &&
- !std::is_signed<T>::value &&
- !std::is_signed<U>::value>::type> {
- using result_type =
- typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type;
+ std::is_integral<U>::value>::type> {
+ using result_type = typename UnsignedIntegerForSize<
+ typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type>::type;
template <typename V = result_type>
static bool Do(T x, U y, V* result) {
- result_type tmp = x | y;
+ result_type tmp = static_cast<result_type>(x) | static_cast<result_type>(y);
*result = static_cast<V>(tmp);
return IsValueInRangeForNumericType<V>(tmp);
}
@@ -649,14 +645,12 @@ template <typename T, typename U>
struct CheckedXorOp<T,
U,
typename std::enable_if<std::is_integral<T>::value &&
- std::is_integral<U>::value &&
- !std::is_signed<T>::value &&
- !std::is_signed<U>::value>::type> {
- using result_type =
- typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type;
+ std::is_integral<U>::value>::type> {
+ using result_type = typename UnsignedIntegerForSize<
+ typename ArithmeticPromotion<MAX_EXPONENT_PROMOTION, T, U>::type>::type;
template <typename V = result_type>
static bool Do(T x, U y, V* result) {
- result_type tmp = x ^ y;
+ result_type tmp = static_cast<result_type>(x) ^ static_cast<result_type>(y);
*result = static_cast<V>(tmp);
return IsValueInRangeForNumericType<V>(tmp);
}
« no previous file with comments | « base/numerics/safe_math.h ('k') | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698