Change exp to int64 to prevent overflow

This commit is contained in:
Thiago Arruda 2015-07-28 08:08:24 -03:00
parent 849f553b9d
commit 3e7f1c747e

View file

@ -81,17 +81,17 @@ func New(value int64, exp int32) Decimal {
// //
func NewFromString(value string) (Decimal, error) { func NewFromString(value string) (Decimal, error) {
var intString string var intString string
var exp int32 var exp int64
// 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, 32) expInt, err := strconv.ParseInt(value[eIndex+1:len(value)], 10, 64)
if err != nil { if err != nil {
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[0:eIndex]
exp = int32(expInt) exp = expInt
} }
parts := strings.Split(value, ".") parts := strings.Split(value, ".")
@ -102,11 +102,7 @@ func NewFromString(value string) (Decimal, error) {
} else if len(parts) == 2 { } else if len(parts) == 2 {
intString = parts[0] + parts[1] intString = parts[0] + parts[1]
expInt := -len(parts[1]) expInt := -len(parts[1])
if expInt < math.MinInt32 { exp += int64(expInt)
// NOTE(vadim): I doubt a string could realistically be this long
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
}
exp += int32(expInt)
} else { } else {
return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value) return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value)
} }
@ -117,9 +113,14 @@ func NewFromString(value string) (Decimal, error) {
return Decimal{}, fmt.Errorf("can't convert %s to decimal", value) return Decimal{}, fmt.Errorf("can't convert %s to decimal", value)
} }
if exp < math.MinInt32 || exp > math.MaxInt32 {
// NOTE(vadim): I doubt a string could realistically be this long
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
}
return Decimal{ return Decimal{
value: dValue, value: dValue,
exp: exp, exp: int32(exp),
}, nil }, nil
} }