Skip to content

Commit

Permalink
go/constant: avoid generating rats for large negative exponents
Browse files Browse the repository at this point in the history
Fixes #20228

Change-Id: I1893ae3e192da01f9befe5469b2a32e534a691ba
Reviewed-on: https://go-review.googlesource.com/42592
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
josharian committed May 4, 2017
1 parent d62c6c3 commit 8d63408
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/go/constant/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ func makeFloatFromLiteral(lit string) Value {
if f, ok := newFloat().SetString(lit); ok {
if smallRat(f) {
// ok to use rationals
if f.Sign() == 0 {
// Issue 20228: If the float underflowed to zero, parse just "0".
// Otherwise, lit might contain a value with a large negative exponent,
// such as -6e-1886451601. As a float, that will underflow to 0,
// but it'll take forever to parse as a Rat.
lit = "0"
}
r, _ := newRat().SetString(lit)
return ratVal{r}
}
Expand Down
3 changes: 2 additions & 1 deletion src/go/constant/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ var stringTests = []struct {
{"1e9999", "1e+9999", "0x.f8d4a9da224650a8cb2959e10d985ad92adbd44c62917e608b1f24c0e1b76b6f61edffeb15c135a4b601637315f7662f325f82325422b244286a07663c9415d2p+33216"},
{"1e-9999", "1e-9999", "0x.83b01ba6d8c0425eec1b21e96f7742d63c2653ed0a024cf8a2f9686df578d7b07d7a83d84df6a2ec70a921d1f6cd5574893a7eda4d28ee719e13a5dce2700759p-33215"},
{"2.71828182845904523536028747135266249775724709369995957496696763", "2.71828", "271828182845904523536028747135266249775724709369995957496696763/100000000000000000000000000000000000000000000000000000000000000"},
{"0e9999999999", "0", "0"}, // issue #16176
{"0e9999999999", "0", "0"}, // issue #16176
{"-6e-1886451601", "0", "0"}, // issue #20228

// Complex
{"0i", "(0 + 0i)", "(0 + 0i)"},
Expand Down

0 comments on commit 8d63408

Please sign in to comment.