diff options
| author | 2026-03-05 21:09:07 +0800 | |
|---|---|---|
| committer | 2026-03-05 21:14:24 +0800 | |
| commit | 355f5b3dc9ae560827cd274e113f43d09ee9ac49 (patch) | |
| tree | 2046f5d51110fff82aecf35c75884222f51ac36c /internal/intconv | |
| parent | objectid, format/pack/ingest: Pack hash ID in algo (diff) | |
| signature | No signature | |
*: Fix overflows
Diffstat (limited to 'internal/intconv')
| -rw-r--r-- | internal/intconv/intconv.go | 27 |
1 files changed, 27 insertions, 0 deletions
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 { |
