Skip to content

Commit

Permalink
num: fix overflow in decodeNumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
thda committed May 28, 2020
1 parent 27c7d91 commit ea69ac8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions num.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Num struct {

// initialise all the possible exponents for numeric datatypes
// Will be mainly used to check for overflow.
var decimalPowers [38]*big.Int
var decimalPowers [41]*big.Int

func init() {
for i := int64(0); i < 38; i++ {
for i := int64(0); i <= 40; i++ {
decimalPowers[i] = new(big.Int).Exp(big.NewInt(10), big.NewInt(i), nil)
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (n Num) String() string {

return strings.Trim(string(str[:len(str)-int(n.scale)])+
"."+string(str[len(str)-
int(n.scale):len(str)]), "0")
int(n.scale):]), "0")
}

// Rat returns the underlying big.Rat value
Expand Down Expand Up @@ -270,6 +270,11 @@ func decodeNumeric(e *binary.Encoder, i colType) (interface{}, error) {
bytes := make([]byte, i.bufferSize-1)
e.Read(bytes)

// safety check
if int(i.scale) > len(decimalPowers)-1 {
return nil, ErrOverFlow
}

// cast as a big.Rat
out := new(big.Rat).SetFrac(new(big.Int).SetBytes(bytes), decimalPowers[i.scale])
if sign != 0 {
Expand Down

0 comments on commit ea69ac8

Please sign in to comment.