tape: Safely cast when dynamically encoding/decoding
This commit is contained in:
@@ -11,3 +11,16 @@ package tape
|
||||
// You shouldn't need to change this. If you do, it should only be set once at
|
||||
// the start of the program.
|
||||
var MaxStructureLength = 1024 * 1024
|
||||
|
||||
// MaxInt is the maximum value an int can hold. This varies depending on the
|
||||
// system.
|
||||
const MaxInt int = int(^uint(0) >> 1)
|
||||
|
||||
// Uint64ToIntSafe casts the input to an int if it can be done without overflow,
|
||||
// or returns an error otherwise.
|
||||
func Uint64ToIntSafe(input uint64) (int, error) {
|
||||
if input > uint64(MaxInt) {
|
||||
return 0, ErrTooLarge
|
||||
}
|
||||
return int(input), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user