Skip to content

Commit

Permalink
topdown: Fix units.parse_bytes implementation to use int64
Browse files Browse the repository at this point in the history
Fixes open-policy-agent#1815

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
  • Loading branch information
tsandall committed Oct 7, 2019
1 parent bc3bf0e commit 27c23c1
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions topdown/parse_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ import (
"github.com/open-policy-agent/opa/topdown/builtins"
)

type multiplier int

const (
none multiplier = 1
kb = 1000
ki = 1024
mb = kb * 1000
mi = ki * 1024
gb = mb * 1000
gi = mi * 1024
tb = gb * 1000
ti = gi * 1024
none int64 = 1
kb = 1000
ki = 1024
mb = kb * 1000
mi = ki * 1024
gb = mb * 1000
gi = mi * 1024
tb = gb * 1000
ti = gi * 1024
)

// The rune values for 0..9 as well as the period symbol (for parsing floats)
Expand All @@ -46,7 +44,7 @@ var (
)

func builtinNumBytes(a ast.Value) (ast.Value, error) {
var m multiplier
var m int64

raw, err := builtins.StringOperand(a, 1)
if err != nil {
Expand Down Expand Up @@ -88,14 +86,14 @@ func builtinNumBytes(a ast.Value) (ast.Value, error) {
return nil, errUnitNotRecognized(unitStr)
}

num, err := strconv.Atoi(numStr)
num, err := strconv.ParseInt(numStr, 10, 64)
if err != nil {
return nil, errIntConv
}

total := num * int(m)
total := num * m

return builtins.IntToNumber(big.NewInt(int64(total))), nil
return builtins.IntToNumber(big.NewInt(total)), nil
}

// Makes the string lower case and removes spaces and quotation marks
Expand Down

0 comments on commit 27c23c1

Please sign in to comment.