safeconv

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package safeconv provides safe integer type conversions with overflow detection. All conversion functions return an error if the value cannot be represented in the target type without overflow. Clamping variants return the nearest valid value instead of an error.

This package exports a comprehensive set of conversion functions for various integer types. While not all functions may be used internally, they are provided as a complete API for safe integer conversions throughout the codebase.

Index

Constants

This section is empty.

Variables

View Source
var ErrOverflow = errors.New("integer overflow during conversion")

ErrOverflow is returned when a conversion would overflow the target type.

Functions

func AddInt

func AddInt(a, b int) (int, error)

AddInt safely adds two int values. Returns ErrOverflow if the result would overflow int.

func ClampInt32ToUint8

func ClampInt32ToUint8(v int32) uint8

ClampInt32ToUint8 converts an int32 to uint8, clamping to the valid range [0, 255].

func ClampInt64ToInt

func ClampInt64ToInt(v int64) int

ClampInt64ToInt converts an int64 to int, clamping to the valid range of int.

func ClampIntToInt32

func ClampIntToInt32(v int) int32

ClampIntToInt32 converts an int to int32, clamping to the valid range [-2147483648, 2147483647].

func ClampIntToUint

func ClampIntToUint(v int) uint

ClampIntToUint converts an int to uint, clamping negative values to 0.

func ClampIntToUint8

func ClampIntToUint8(v int) uint8

ClampIntToUint8 converts an int to uint8, clamping to the valid range [0, 255].

func ClampIntToUint16

func ClampIntToUint16(v int) uint16

ClampIntToUint16 converts an int to uint16, clamping to the valid range [0, 65535].

func ClampIntToUint32

func ClampIntToUint32(v int) uint32

ClampIntToUint32 converts an int to uint32, clamping to the valid range [0, 4294967295].

func ClampToInt8

func ClampToInt8(v int64) int8

ClampToInt8 converts an int64 to int8, clamping to the valid range [-128, 127].

func ClampToInt16

func ClampToInt16(v int64) int16

ClampToInt16 converts an int64 to int16, clamping to the valid range [-32768, 32767].

func ClampToInt32

func ClampToInt32(v int64) int32

ClampToInt32 converts an int64 to int32, clamping to the valid range [-2147483648, 2147483647].

func ClampToUint8

func ClampToUint8(v int64) uint8

ClampToUint8 converts an int64 to uint8, clamping to the valid range [0, 255].

func ClampToUint16

func ClampToUint16(v int64) uint16

ClampToUint16 converts an int64 to uint16, clamping to the valid range [0, 65535].

func ClampToUint32

func ClampToUint32(v int64) uint32

ClampToUint32 converts an int64 to uint32, clamping to the valid range [0, 4294967295].

func ClampToUint64

func ClampToUint64(v int64) uint64

ClampToUint64 converts an int64 to uint64, clamping negative values to 0.

func ClampUint16ToUint8

func ClampUint16ToUint8(v uint16) uint8

ClampUint16ToUint8 converts a uint16 to uint8, clamping to 255.

func ClampUint32ToUint8

func ClampUint32ToUint8(v uint32) uint8

ClampUint32ToUint8 converts a uint32 to uint8, clamping to 255.

func ClampUint32ToUint16

func ClampUint32ToUint16(v uint32) uint16

ClampUint32ToUint16 converts a uint32 to uint16, clamping to 65535.

func ClampUint64ToUint32

func ClampUint64ToUint32(v uint64) uint32

ClampUint64ToUint32 converts a uint64 to uint32, clamping to MaxUint32.

func Int32ToUint8

func Int32ToUint8(v int32) (uint8, error)

Int32ToUint8 safely converts an int32 to uint8. Returns ErrOverflow if the value is outside the range [0, 255].

func Int64ToInt

func Int64ToInt(v int64) (int, error)

Int64ToInt safely converts an int64 to int. Returns ErrOverflow if the value is outside the range of int. Note: On 64-bit systems, int is 64 bits, so this never overflows. On 32-bit systems, int is 32 bits, so this checks the int32 range.

func IntToInt32

func IntToInt32(v int) (int32, error)

IntToInt32 safely converts an int to int32. Returns ErrOverflow if the value is outside the range [-2147483648, 2147483647].

func IntToUint

func IntToUint(v int) (uint, error)

IntToUint safely converts an int to uint. Returns ErrOverflow if the value is negative.

func IntToUint8

func IntToUint8(v int) (uint8, error)

IntToUint8 safely converts an int to uint8. Returns ErrOverflow if the value is outside the range [0, 255].

func IntToUint16

func IntToUint16(v int) (uint16, error)

IntToUint16 safely converts an int to uint16. Returns ErrOverflow if the value is outside the range [0, 65535].

func IntToUint32

func IntToUint32(v int) (uint32, error)

IntToUint32 safely converts an int to uint32. Returns ErrOverflow if the value is outside the range [0, 4294967295].

func IntToUint64

func IntToUint64(v int) (uint64, error)

IntToUint64 safely converts an int to uint64. Returns ErrOverflow if the value is negative.

func MultiplyInt

func MultiplyInt(a, b int) (int, error)

MultiplyInt safely multiplies two int values. Returns ErrOverflow if the result would overflow int.

func MustInt64ToInt

func MustInt64ToInt(v int64) int

MustInt64ToInt converts an int64 to int, panicking on overflow. Use only when overflow is logically impossible.

func MustIntToInt32

func MustIntToInt32(v int) int32

MustIntToInt32 converts an int to int32, panicking on overflow. Use only when overflow is logically impossible.

func MustIntToUint

func MustIntToUint(v int) uint

MustIntToUint converts an int to uint, panicking on overflow. Use only when the value is guaranteed to be non-negative.

func MustIntToUint8

func MustIntToUint8(v int) uint8

MustIntToUint8 converts an int to uint8, panicking on overflow. Use only when overflow is logically impossible.

func MustIntToUint16

func MustIntToUint16(v int) uint16

MustIntToUint16 converts an int to uint16, panicking on overflow. Use only when overflow is logically impossible.

func MustIntToUint32

func MustIntToUint32(v int) uint32

MustIntToUint32 converts an int to uint32, panicking on overflow. Use only when overflow is logically impossible.

func MustIntToUint64

func MustIntToUint64(v int) uint64

MustIntToUint64 converts an int to uint64, panicking on overflow. Use only when the value is guaranteed to be non-negative.

func MustToInt8

func MustToInt8(v int64) int8

MustToInt8 converts an int64 to int8, panicking on overflow. Use only when overflow is logically impossible.

func MustToInt16

func MustToInt16(v int64) int16

MustToInt16 converts an int64 to int16, panicking on overflow. Use only when overflow is logically impossible.

func MustToInt32

func MustToInt32(v int64) int32

MustToInt32 converts an int64 to int32, panicking on overflow. Use only when overflow is logically impossible.

func MustToUint8

func MustToUint8(v int64) uint8

MustToUint8 converts an int64 to uint8, panicking on overflow. Use only when overflow is logically impossible (e.g., value is from a validated source or is the result of a modulo operation that guarantees the range).

func MustToUint16

func MustToUint16(v int64) uint16

MustToUint16 converts an int64 to uint16, panicking on overflow. Use only when overflow is logically impossible.

func MustToUint32

func MustToUint32(v int64) uint32

MustToUint32 converts an int64 to uint32, panicking on overflow. Use only when overflow is logically impossible.

func MustToUint64

func MustToUint64(v int64) uint64

MustToUint64 converts an int64 to uint64, panicking on overflow. Use only when the value is guaranteed to be non-negative.

func MustUint64ToInt

func MustUint64ToInt(v uint64) int

MustUint64ToInt converts a uint64 to int, panicking on overflow. Use only when overflow is logically impossible.

func MustUint64ToInt64

func MustUint64ToInt64(v uint64) int64

MustUint64ToInt64 converts a uint64 to int64, panicking on overflow. Use only when overflow is logically impossible.

func MustUint64ToUint32

func MustUint64ToUint32(v uint64) uint32

MustUint64ToUint32 converts a uint64 to uint32, panicking on overflow. Use only when overflow is logically impossible.

func SafeShiftAmount

func SafeShiftAmount(shift int) uint

SafeShiftAmount clamps a shift amount to a safe range [0, 63] to prevent panics from shifting by >= 64 bits. For JPEG, precision is typically 8-16, so this only affects malformed/malicious inputs.

func ToInt

func ToInt(v uint64) (int, error)

ToInt safely converts a uint64 to int. Returns ErrOverflow if the value exceeds MaxInt.

func ToInt8

func ToInt8(v int64) (int8, error)

ToInt8 safely converts an int64 to int8. Returns ErrOverflow if the value is outside the range [-128, 127].

func ToInt16

func ToInt16(v int64) (int16, error)

ToInt16 safely converts an int64 to int16. Returns ErrOverflow if the value is outside the range [-32768, 32767].

func ToInt32

func ToInt32(v int64) (int32, error)

ToInt32 safely converts an int64 to int32. Returns ErrOverflow if the value is outside the range [-2147483648, 2147483647].

func ToInt64

func ToInt64(v uint64) (int64, error)

ToInt64 safely converts a uint64 to int64. Returns ErrOverflow if the value exceeds MaxInt64.

func ToUint

func ToUint(v int64) (uint, error)

ToUint safely converts an int64 to uint. Returns ErrOverflow if the value is negative.

func ToUint8

func ToUint8(v int64) (uint8, error)

ToUint8 safely converts an int64 to uint8. Returns ErrOverflow if the value is outside the range [0, 255].

func ToUint16

func ToUint16(v int64) (uint16, error)

ToUint16 safely converts an int64 to uint16. Returns ErrOverflow if the value is outside the range [0, 65535].

func ToUint32

func ToUint32(v int64) (uint32, error)

ToUint32 safely converts an int64 to uint32. Returns ErrOverflow if the value is outside the range [0, 4294967295].

func ToUint64

func ToUint64(v int64) (uint64, error)

ToUint64 safely converts an int64 to uint64. Returns ErrOverflow if the value is negative.

func Uint16ToInt

func Uint16ToInt(v uint16) (int, error)

Uint16ToInt safely converts a uint16 to int. This conversion is always safe as uint16 max (65535) fits in int.

func Uint16ToInt16

func Uint16ToInt16(v uint16) int16

Uint16ToInt16 reinterprets a uint16 as an int16 preserving the bit pattern. This is used for binary formats where unsigned bytes represent signed values. Values 0-32767 map to 0-32767, values 32768-65535 map to -32768 to -1. This conversion is intentional bit reinterpretation and never overflows.

func Uint16ToUint8

func Uint16ToUint8(v uint16) (uint8, error)

Uint16ToUint8 safely converts a uint16 to uint8. Returns ErrOverflow if the value exceeds 255.

func Uint32ToInt

func Uint32ToInt(v uint32) (int, error)

Uint32ToInt safely converts a uint32 to int. Returns ErrOverflow if the value exceeds MaxInt (on 32-bit systems).

func Uint32ToUint8

func Uint32ToUint8(v uint32) (uint8, error)

Uint32ToUint8 safely converts a uint32 to uint8. Returns ErrOverflow if the value exceeds 255.

func Uint32ToUint16

func Uint32ToUint16(v uint32) (uint16, error)

Uint32ToUint16 safely converts a uint32 to uint16. Returns ErrOverflow if the value exceeds 65535.

func Uint64ToInt

func Uint64ToInt(v uint64) (int, error)

Uint64ToInt safely converts a uint64 to int. Returns ErrOverflow if the value exceeds MaxInt.

func Uint64ToInt64

func Uint64ToInt64(v uint64) (int64, error)

Uint64ToInt64 safely converts a uint64 to int64. Returns ErrOverflow if the value exceeds MaxInt64.

func Uint64ToUint32

func Uint64ToUint32(v uint64) (uint32, error)

Uint64ToUint32 safely converts a uint64 to uint32. Returns ErrOverflow if the value exceeds MaxUint32.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL