Bug 768538 - 1/2 - Fix undefined behavior in CheckedInt - r=jwalden
authorBenoit Jacob <bjacob@mozilla.com>
Thu, 05 Jul 2012 10:13:13 -0400
changeset 98377 d0d426929fda5dd02a5204f87d5a7b9d38745c1a
parent 98376 53699dc2704768959772eb10876eb77134af40c6
child 98378 79f74e4d885c1616f51c33d63502b6e37f37bb1a
push id11461
push userbjacob@mozilla.com
push dateThu, 05 Jul 2012 14:15:09 +0000
treeherdermozilla-inbound@bf32387bfec3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwalden
bugs768538
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 768538 - 1/2 - Fix undefined behavior in CheckedInt - r=jwalden
mfbt/CheckedInt.h
--- a/mfbt/CheckedInt.h
+++ b/mfbt/CheckedInt.h
@@ -673,21 +673,23 @@ class CheckedInt
 
 #define MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(NAME, OP)                \
 template<typename T>                                                  \
 inline CheckedInt<T> operator OP(const CheckedInt<T> &lhs,            \
                                  const CheckedInt<T> &rhs)            \
 {                                                                     \
   T x = lhs.mValue;                                                   \
   T y = rhs.mValue;                                                   \
-  T result = x OP y;                                                  \
   T isOpValid = detail::Is##NAME##Valid(x, y);                        \
-  /* Help the compiler perform RVO (return value optimization). */    \
-  return CheckedInt<T>(result,                                        \
-                       lhs.mIsValid && rhs.mIsValid && isOpValid);    \
+  if (isOpValid) {                                                    \
+    T result = x OP y;                                                \
+    return CheckedInt<T>(result, lhs.mIsValid && rhs.mIsValid);       \
+  } else {                                                            \
+    return CheckedInt<T>(T(0), false);                                \
+  }                                                                   \
 }
 
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Add, +)
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Sub, -)
 MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR(Mul, *)
 
 #undef MOZ_CHECKEDINT_BASIC_BINARY_OPERATOR