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

Unified Diff: Source/platform/Decimal.cpp

Issue 977593004: Avoid uint64_t overflow in Decimal::operator/() and fix Decimal::ceil() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 2015-03-04T15:05:41 Created 5 years, 10 months 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 | « no previous file | Source/platform/DecimalTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/Decimal.cpp
diff --git a/Source/platform/Decimal.cpp b/Source/platform/Decimal.cpp
index 35b5a8aaf4e6ccb7b197748fb647de2968e950fc..97013153dc5eabf11bde42d383cb1bb0b03d6eda 100644
--- a/Source/platform/Decimal.cpp
+++ b/Source/platform/Decimal.cpp
@@ -488,13 +488,18 @@ Decimal Decimal::operator/(const Decimal& rhs) const
uint64_t remainder = lhs.m_data.coefficient();
const uint64_t divisor = rhs.m_data.coefficient();
uint64_t result = 0;
- while (result < MaxCoefficient / 100) {
- while (remainder < divisor) {
+ for (;;) {
+ while (remainder < divisor && result < MaxCoefficient / 10) {
remainder *= 10;
result *= 10;
--resultExponent;
}
- result += remainder / divisor;
+ if (remainder < divisor)
+ break;
+ uint64_t quotient = remainder / divisor;
+ if (result > MaxCoefficient - quotient)
+ break;
+ result += quotient;
remainder %= divisor;
if (!remainder)
break;
@@ -628,7 +633,7 @@ Decimal Decimal::ceil() const
uint64_t result = m_data.coefficient();
const int numberOfDigits = countDigits(result);
const int numberOfDropDigits = -exponent();
- if (numberOfDigits < numberOfDropDigits)
+ if (numberOfDigits <= numberOfDropDigits)
return isPositive() ? Decimal(1) : zero(Positive);
result = scaleDown(result, numberOfDropDigits);
« no previous file with comments | « no previous file | Source/platform/DecimalTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698