Skip to content

Commit

Permalink
Limit CPU and memory usage for int-to-float conversion
Browse files Browse the repository at this point in the history
Co-authored-by: kcza <[email protected]>
  • Loading branch information
marco6 and TheSignPainter98 committed Oct 16, 2023
1 parent 47c85ba commit dbbf4f0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions starlark/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ func (i Int) Float() Float {
return Float(iBig.Uint64())
} else if iBig.IsInt64() {
return Float(iBig.Int64())
} else {
// Fast path for very big ints.
const maxFiniteLen = 1023 + 1 // max exponent value + implicit mantissa bit
if iBig.BitLen() > maxFiniteLen {
return Float(math.Inf(iBig.Sign()))
}
}

f, _ := new(big.Float).SetInt(iBig).Float64()
Expand Down

0 comments on commit dbbf4f0

Please sign in to comment.