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 98411 d0d426929fda5dd02a5204f87d5a7b9d38745c1a
parent 98410 53699dc2704768959772eb10876eb77134af40c6
child 98412 79f74e4d885c1616f51c33d63502b6e37f37bb1a
push idunknown
push userunknown
push dateunknown
reviewersjwalden
bugs768538
milestone16.0a1
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