From 355f5b3dc9ae560827cd274e113f43d09ee9ac49 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 5 Mar 2026 21:09:07 +0800 Subject: *: Fix overflows --- internal/intconv/intconv.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'internal/intconv') diff --git a/internal/intconv/intconv.go b/internal/intconv/intconv.go index 67f99a14..c4b429e3 100644 --- a/internal/intconv/intconv.go +++ b/internal/intconv/intconv.go @@ -33,6 +33,33 @@ func IntToUint64(v int) (uint64, error) { return uint64(v), nil } +// IntToUint32 converts v to uint32, returning an error if it overflows. +func IntToUint32(v int) (uint32, error) { + if v < 0 || v > math.MaxUint32 { + return 0, fmt.Errorf("intconv: int %d overflows uint32", v) + } + + return uint32(v), nil +} + +// Uint64ToInt64 converts v to int64, returning an error if it overflows. +func Uint64ToInt64(v uint64) (int64, error) { + if v > math.MaxInt64 { + return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v) + } + + return int64(v), nil +} + +// Int64ToUint64 converts v to uint64, returning an error if v is negative. +func Int64ToUint64(v int64) (uint64, error) { + if v < 0 { + return 0, fmt.Errorf("intconv: int64 %d is negative", v) + } + + return uint64(v), nil +} + // Int64ToInt32 converts v to int32, returning an error if it overflows. func Int64ToInt32(v int64) (int32, error) { if v < math.MinInt32 || v > math.MaxInt32 { -- cgit v1.3.1-10-gc9f91