Fix possible int64 overflow and remove unnecessary code

This commit is contained in:
Thiago Arruda 2015-07-31 13:36:54 -03:00
parent 669901768e
commit 632e313f4a
2 changed files with 8 additions and 2 deletions

View file

@ -86,11 +86,15 @@ func NewFromString(value string) (Decimal, error) {
// Check if number is using scientific notation // Check if number is using scientific notation
eIndex := strings.IndexAny(value, "Ee") eIndex := strings.IndexAny(value, "Ee")
if eIndex != -1 { if eIndex != -1 {
expInt, err := strconv.ParseInt(value[eIndex+1:len(value)], 10, 64) expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32)
if err != nil { if err != nil {
rerr := err.(*strconv.NumError)
if rerr.Err == strconv.ErrRange {
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
}
return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value) return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value)
} }
value = value[0:eIndex] value = value[:eIndex]
exp = expInt exp = expInt
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"math" "math"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -135,6 +136,7 @@ func TestNewFromStringErrs(t *testing.T) {
"123.456e1.3", "123.456e1.3",
"1e-1.2", "1e-1.2",
"123.456e-1.3", "123.456e-1.3",
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
} }
for _, s := range tests { for _, s := range tests {