Index: src/dateparser.cc |
diff --git a/src/dateparser.cc b/src/dateparser.cc |
index 09dbf1127dbf19f604ce99336f8936a4d82b4d7e..d096a7ec9fc58ce24c970075b09b2409357fb972 100644 |
--- a/src/dateparser.cc |
+++ b/src/dateparser.cc |
@@ -100,8 +100,15 @@ bool DateParser::TimeZoneComposer::Write(FixedArray* output) { |
if (sign_ != kNone) { |
if (hour_ == kNone) hour_ = 0; |
if (minute_ == kNone) minute_ = 0; |
- int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60); |
- if (!Smi::IsValid(total_seconds)) return false; |
+ // Avoid signed integer overflow (undefined behavior) by doing unsigned |
+ // arithmetic. |
+ unsigned total_seconds_unsigned = hour_ * 3600U + minute_ * 60U; |
+ if (total_seconds_unsigned > Smi::kMaxValue) return false; |
+ int total_seconds = static_cast<int>(total_seconds_unsigned); |
+ if (sign_ < 0) { |
+ total_seconds = -total_seconds; |
+ } |
+ DCHECK(Smi::IsValid(total_seconds)); |
output->set(UTC_OFFSET, Smi::FromInt(total_seconds)); |
} else { |
output->set_null(UTC_OFFSET); |