From abef5cb3edda72d789bd19104f5f2172f4377448 Mon Sep 17 00:00:00 2001 From: Srdjan Mitrovic Date: Wed, 2 Dec 2015 08:45:41 -0800 Subject: [PATCH] Correct overflow check; signed integer overflow is undefined in C++. BUG= R=fschneider@google.com Review URL: https://codereview.chromium.org/1490853002 . --- runtime/vm/object.cc | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc index a0e5ccad6ab..9e68eccf9c5 100644 --- a/runtime/vm/object.cc +++ b/runtime/vm/object.cc @@ -17293,24 +17293,13 @@ RawInteger* Integer::ArithmeticOp(Token::Kind operation, const int64_t right_value = other.AsInt64Value(); switch (operation) { case Token::kADD: { - if (((left_value < 0) != (right_value < 0)) || - ((left_value + right_value) < 0) == (left_value < 0)) { + if (!Utils::WillAddOverflow(left_value, right_value)) { return Integer::New(left_value + right_value, space); } break; } case Token::kSUB: { - // TODO(srdjan): XCode 7 produces different code in -O0 than in -O2 for - // following code when 'left_value - right_value' overflows into a - // positive number (left negative, right positive): - // if (((left_value < 0) == (right_value < 0)) || - // ((left_value - right_value) < 0) == (left_value < 0)) { - // - // Restructuring code using temporary variables is a workaround. - const bool both_same_sign = (left_value < 0) == (right_value < 0); - const bool result_same_sign_as_left = - ((left_value - right_value) < 0) == (left_value < 0); - if (both_same_sign || result_same_sign_as_left) { + if (!Utils::WillSubOverflow(left_value, right_value)) { return Integer::New(left_value - right_value, space); } break;