mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 11:45:18 +00:00
ints: stricter numbers (#4939)
This commit is contained in:
41
libs/math/safemath.go
Normal file
41
libs/math/safemath.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package math
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
var ErrOverflowInt32 = errors.New("int32 overflow")
|
||||
|
||||
// SafeAddInt32 adds two int32 integers
|
||||
// If there is an overflow this will panic
|
||||
func SafeAddInt32(a, b int32) int32 {
|
||||
if b > 0 && (a > math.MaxInt32-b) {
|
||||
panic(ErrOverflowInt32)
|
||||
} else if b < 0 && (a < math.MinInt32-b) {
|
||||
panic(ErrOverflowInt32)
|
||||
}
|
||||
return a + b
|
||||
}
|
||||
|
||||
// SafeSubInt32 subtracts two int32 integers
|
||||
// If there is an overflow this will panic
|
||||
func SafeSubInt32(a, b int32) int32 {
|
||||
if b > 0 && (a < math.MinInt32+b) {
|
||||
panic(ErrOverflowInt32)
|
||||
} else if b < 0 && (a > math.MaxInt32+b) {
|
||||
panic(ErrOverflowInt32)
|
||||
}
|
||||
return a - b
|
||||
}
|
||||
|
||||
// SafeConvertInt32 takes a int and checks if it overflows
|
||||
// If there is an overflow this will panic
|
||||
func SafeConvertInt32(a int64) int32 {
|
||||
if a > math.MaxInt32 {
|
||||
panic(ErrOverflowInt32)
|
||||
} else if a < math.MinInt32 {
|
||||
panic(ErrOverflowInt32)
|
||||
}
|
||||
return int32(a)
|
||||
}
|
||||
Reference in New Issue
Block a user