Documentation
¶
Overview ¶
Package safeconv provides safe integer conversion functions that check bounds before converting between integer types, preventing silent overflow bugs.
These helpers address gosec G115 warnings by making bounds explicit.
Index ¶
- func Int8ToByte(v int8) byte
- func Int64ToUint64(v int64) (uint64, error)
- func Int64ToUint64OrZero(v int64) uint64
- func IntToUintptr(n int) (uintptr, error)
- func IntToUintptrOrZero(n int) uintptr
- func RuneToByte(r rune) (byte, error)
- func RuneToByteOrDefault(r rune, defaultVal byte) byte
- func ToInt16(v int) (int16, error)
- func ToInt16OrDefault(v int, defaultVal int16) int16
- func ToInt32(v int) (int32, error)
- func ToInt32OrDefault(v int, defaultVal int32) int32
- func ToPort(v int) (uint16, error)
- func ToPortOrDefault(v int, defaultPort uint16) uint16
- func ToUint32(n int) (uint32, error)
- func ToUint32OrZero(n int) uint32
- func ToUint64(n int) (uint64, error)
- func ToUint64OrZero(n int) uint64
- func Uint64ToInt64(v uint64) (int64, error)
- func Uint64ToInt64OrMax(v uint64) int64
- func UintToInt64(v uint) (int64, error)
- func UintToInt64OrMax(v uint) int64
- func UintptrToInt(u uintptr) (int, error)
- func UintptrToIntOrZero(u uintptr) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Int8ToByte ¶
Int8ToByte converts int8 to byte (always safe, just changes interpretation). Included for consistency; negative int8 becomes high byte values.
func Int64ToUint64 ¶
Int64ToUint64 converts int64 to uint64 with non-negative check. Use for timestamps, byte counts, etc.
func Int64ToUint64OrZero ¶
Int64ToUint64OrZero converts int64 to uint64, returning 0 if negative.
func IntToUintptr ¶
IntToUintptr converts int to uintptr with non-negative check.
func IntToUintptrOrZero ¶
IntToUintptrOrZero converts int to uintptr, returning 0 if negative.
func RuneToByte ¶
RuneToByte converts rune to byte with bounds check. Only ASCII characters (0-127) are valid.
func RuneToByteOrDefault ¶
RuneToByteOrDefault converts rune to byte, returning defaultVal for non-ASCII.
func ToInt16 ¶
ToInt16 converts int to int16 with bounds checking. Use for config values known to be small (e.g., small counts, flags).
func ToInt16OrDefault ¶
ToInt16OrDefault converts int to int16, returning defaultVal on overflow. Use when a fallback is acceptable (e.g., config with defaults).
func ToInt32 ¶
ToInt32 converts int to int32 with bounds checking. Use for IDs, partition numbers, etc.
func ToInt32OrDefault ¶
ToInt32OrDefault converts int to int32, returning defaultVal on overflow.
func ToPortOrDefault ¶
ToPortOrDefault converts int to port, returning defaultPort on invalid.
func ToUint32 ¶
ToUint32 converts int to uint32 with bounds checking. Use for sizes, counts, and other non-negative bounded values.
func ToUint32OrZero ¶
ToUint32OrZero converts int to uint32, returning 0 on invalid input.
func ToUint64 ¶
ToUint64 converts int to uint64 with non-negative check. Use for sizes, lengths, and counts.
func ToUint64OrZero ¶
ToUint64OrZero converts int to uint64, returning 0 if negative.
func Uint64ToInt64 ¶
Uint64ToInt64 converts uint64 to int64 with overflow check.
func Uint64ToInt64OrMax ¶
Uint64ToInt64OrMax converts uint64 to int64, capping at MaxInt64.
func UintToInt64 ¶
UintToInt64 converts uint to int64 with overflow check. On 64-bit systems, uint can exceed MaxInt64.
func UintToInt64OrMax ¶
UintToInt64OrMax converts uint to int64, capping at MaxInt64 on overflow.
func UintptrToInt ¶
UintptrToInt converts uintptr to int with overflow check. Use for memory mapping / pointer math on 64-bit systems.
func UintptrToIntOrZero ¶
UintptrToIntOrZero converts uintptr to int, returning 0 on overflow.
Types ¶
This section is empty.