Skip to content

Commit

Permalink
Fix parsing of exponents in StringToDouble.
Browse files Browse the repository at this point in the history
BUG=542881

Review-Url: https://codereview.chromium.org/2296503003
Cr-Commit-Position: refs/heads/master@{#416158}
  • Loading branch information
leizleiz authored and Commit bot committed Sep 2, 2016
1 parent e11300e commit 079cdc8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
34 changes: 34 additions & 0 deletions base/strings/string_number_conversions_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -719,24 +719,58 @@ TEST(StringNumberConversionsTest, StringToDouble) {
double output;
bool success;
} cases[] = {
// Test different forms of zero.
{"0", 0.0, true},
{"+0", 0.0, true},
{"-0", 0.0, true},
{"0.0", 0.0, true},
{"000000000000000000000000000000.0", 0.0, true},
{"0.000000000000000000000000000", 0.0, true},

// Test the answer.
{"42", 42.0, true},
{"-42", -42.0, true},

// Test variances of an ordinary number.
{"123.45", 123.45, true},
{"-123.45", -123.45, true},
{"+123.45", 123.45, true},

// Test different forms of representation.
{"2.99792458e8", 299792458.0, true},
{"149597870.691E+3", 149597870691.0, true},
{"6.", 6.0, true},

// Test around the largest/smallest value that a double can represent.
{"9e307", 9e307, true},
{"1.7976e308", 1.7976e308, true},
{"1.7977e308", HUGE_VAL, false},
{"9e308", HUGE_VAL, false},
{"9e309", HUGE_VAL, false},
{"9e999", HUGE_VAL, false},
{"9e1999", HUGE_VAL, false},
{"9e19999", HUGE_VAL, false},
{"9e99999999999999999999", HUGE_VAL, false},
{"-9e307", -9e307, true},
{"-1.7976e308", -1.7976e308, true},
{"-1.7977e308", -HUGE_VAL, false},
{"-9e308", -HUGE_VAL, false},
{"-9e309", -HUGE_VAL, false},
{"-9e999", -HUGE_VAL, false},
{"-9e1999", -HUGE_VAL, false},
{"-9e19999", -HUGE_VAL, false},
{"-9e99999999999999999999", -HUGE_VAL, false},

// Test more exponents.
{"1e-2", 0.01, true},
{"42 ", 42.0, false},
{" 1e-2", 0.01, false},
{"1e-2 ", 0.01, false},
{"-1E-7", -0.0000001, true},
{"01e02", 100, true},
{"2.3e15", 2.3e15, true},

// Test some invalid cases.
{"\t\n\v\f\r -123.45e2", -12345.0, false},
{"+123 e4", 123.0, false},
{"123e ", 123.0, false},
Expand Down
3 changes: 2 additions & 1 deletion base/third_party/dmg_fp/README.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ List of changes made to original code:
- made minor changes for -Wextra for Mac build, see mac_wextra.patch
- crash fix for running with reduced CPU float precision, see
float_precision_crash.patch and crbug.com/123157
- fixed warnings under msvc, see msvc_warnings.patch
- fixed warnings under msvc, see msvc_warnings.patch
- fixed parsing of long exponents, see exp_length.patch and crbug.com/542881
5 changes: 4 additions & 1 deletion base/third_party/dmg_fp/dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2597,8 +2597,11 @@ strtod
if (c > '0' && c <= '9') {
L = c - '0';
s1 = s;
while((c = *++s) >= '0' && c <= '9')
while((c = *++s) >= '0' && c <= '9') {
L = 10*L + c - '0';
if (L > DBL_MAX_10_EXP)
break;
}
if (s - s1 > 8 || L > 19999)
/* Avoid confusion from exponents
* so large that e might overflow.
Expand Down
17 changes: 17 additions & 0 deletions base/third_party/dmg_fp/exp_length.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/base/third_party/dmg_fp/dtoa.cc b/base/third_party/dmg_fp/dtoa.cc
index 502c16c..f3d793e 100644
--- a/base/third_party/dmg_fp/dtoa.cc
+++ b/base/third_party/dmg_fp/dtoa.cc
@@ -2597,8 +2597,11 @@ strtod
if (c > '0' && c <= '9') {
L = c - '0';
s1 = s;
- while((c = *++s) >= '0' && c <= '9')
+ while((c = *++s) >= '0' && c <= '9') {
L = 10*L + c - '0';
+ if (L > DBL_MAX_10_EXP)
+ break;
+ }
if (s - s1 > 8 || L > 19999)
/* Avoid confusion from exponents
* so large that e might overflow.

0 comments on commit 079cdc8

Please sign in to comment.