google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.27k stars 204 forks source link

int(9223372036854775807.5) == -9223372036854775808 #375

Closed adonovan closed 3 years ago

adonovan commented 3 years ago

Due to inexact int/float comparison at https://github.com/google/starlark-go/blob/master/starlark/int.go#L427.

The root cause is that math.MaxInt64 0x7fffffffffffffff cannot be exactly represented as a float64 because it has more than 53 bits of precision, so the comparison f <= MaxInt64 actually compares f to the value float64(MaxInt64), which is numerically equal to MaxInt64+1, which of course cannot be exactly represented as an int64. So if f == MaxInt64 the condition is spuriously true. The fix is to add an extra check for this particular value.

Pointed out by @algebra8 in https://github.com/google/starlark-go/pull/373/files/3aea0fb3460e9c3764a2d287b573fa1a0465a90d..a73eaa2e67bd3236c52f8ed9c0e22d36b7e8d510