safeconv

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MPL-2.0 Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Int8ToByte

func Int8ToByte(v int8) byte

Int8ToByte converts int8 to byte (always safe, just changes interpretation). Included for consistency; negative int8 becomes high byte values.

func Int64ToUint64

func Int64ToUint64(v int64) (uint64, error)

Int64ToUint64 converts int64 to uint64 with non-negative check. Use for timestamps, byte counts, etc.

func Int64ToUint64OrZero

func Int64ToUint64OrZero(v int64) uint64

Int64ToUint64OrZero converts int64 to uint64, returning 0 if negative.

func IntToUintptr

func IntToUintptr(n int) (uintptr, error)

IntToUintptr converts int to uintptr with non-negative check.

func IntToUintptrOrZero

func IntToUintptrOrZero(n int) uintptr

IntToUintptrOrZero converts int to uintptr, returning 0 if negative.

func RuneToByte

func RuneToByte(r rune) (byte, error)

RuneToByte converts rune to byte with bounds check. Only ASCII characters (0-127) are valid.

func RuneToByteOrDefault

func RuneToByteOrDefault(r rune, defaultVal byte) byte

RuneToByteOrDefault converts rune to byte, returning defaultVal for non-ASCII.

func ToInt16

func ToInt16(v int) (int16, error)

ToInt16 converts int to int16 with bounds checking. Use for config values known to be small (e.g., small counts, flags).

func ToInt16OrDefault

func ToInt16OrDefault(v int, defaultVal int16) int16

ToInt16OrDefault converts int to int16, returning defaultVal on overflow. Use when a fallback is acceptable (e.g., config with defaults).

func ToInt32

func ToInt32(v int) (int32, error)

ToInt32 converts int to int32 with bounds checking. Use for IDs, partition numbers, etc.

func ToInt32OrDefault

func ToInt32OrDefault(v int, defaultVal int32) int32

ToInt32OrDefault converts int to int32, returning defaultVal on overflow.

func ToPort

func ToPort(v int) (uint16, error)

ToPort converts int to uint16 port number with validation. Valid range: 0-65535

func ToPortOrDefault

func ToPortOrDefault(v int, defaultPort uint16) uint16

ToPortOrDefault converts int to port, returning defaultPort on invalid.

func ToUint32

func ToUint32(n int) (uint32, error)

ToUint32 converts int to uint32 with bounds checking. Use for sizes, counts, and other non-negative bounded values.

func ToUint32OrZero

func ToUint32OrZero(n int) uint32

ToUint32OrZero converts int to uint32, returning 0 on invalid input.

func ToUint64

func ToUint64(n int) (uint64, error)

ToUint64 converts int to uint64 with non-negative check. Use for sizes, lengths, and counts.

func ToUint64OrZero

func ToUint64OrZero(n int) uint64

ToUint64OrZero converts int to uint64, returning 0 if negative.

func Uint64ToInt64

func Uint64ToInt64(v uint64) (int64, error)

Uint64ToInt64 converts uint64 to int64 with overflow check.

func Uint64ToInt64OrMax

func Uint64ToInt64OrMax(v uint64) int64

Uint64ToInt64OrMax converts uint64 to int64, capping at MaxInt64.

func UintToInt64

func UintToInt64(v uint) (int64, error)

UintToInt64 converts uint to int64 with overflow check. On 64-bit systems, uint can exceed MaxInt64.

func UintToInt64OrMax

func UintToInt64OrMax(v uint) int64

UintToInt64OrMax converts uint to int64, capping at MaxInt64 on overflow.

func UintptrToInt

func UintptrToInt(u uintptr) (int, error)

UintptrToInt converts uintptr to int with overflow check. Use for memory mapping / pointer math on 64-bit systems.

func UintptrToIntOrZero

func UintptrToIntOrZero(u uintptr) int

UintptrToIntOrZero converts uintptr to int, returning 0 on overflow.

Types

This section is empty.

Jump to

Keyboard shortcuts

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