Commit d220c995 authored by Shenghou Ma's avatar Shenghou Ma Committed by Russ Cox

time: fix ParseDuration overflow when given more than 9 digits on 32-bit arch

Fixes #6617.

R=golang-dev, rsc, r
CC=golang-dev
https://golang.org/cl/15080043
parent 580ea8b5
...@@ -1204,11 +1204,11 @@ func ParseDuration(s string) (Duration, error) { ...@@ -1204,11 +1204,11 @@ func ParseDuration(s string) (Duration, error) {
if err != nil { if err != nil {
return 0, errors.New("time: invalid duration " + orig) return 0, errors.New("time: invalid duration " + orig)
} }
scale := 1 scale := 1.0
for n := pl - len(s); n > 0; n-- { for n := pl - len(s); n > 0; n-- {
scale *= 10 scale *= 10
} }
g += float64(x) / float64(scale) g += float64(x) / scale
post = pl != len(s) post = pl != len(s)
} }
if !pre && !post { if !pre && !post {
......
...@@ -1318,6 +1318,8 @@ var parseDurationTests = []struct { ...@@ -1318,6 +1318,8 @@ var parseDurationTests = []struct {
{"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond}, {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
// large value // large value
{"52763797000ns", true, 52763797000 * Nanosecond}, {"52763797000ns", true, 52763797000 * Nanosecond},
// more than 9 digits after decimal point, see http://golang.org/issue/6617
{"0.3333333333333333333h", true, 20 * Minute},
// errors // errors
{"", false, 0}, {"", false, 0},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment