safecast

package
v1.77.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 1 Imported by: 1

README

Safecast

the purpose of this utilities is to perform safe number conversion in go similarly to go-safecast from which they are inspired from. It should help tackling gosec G115 rule

G115: Potential overflow when converting between integer types.

and CWE-190

infinite loop
access to wrong resource by id
grant access to someone who exhausted their quota

Contrary to go-safecast no error is returned when attempting casting and the MAX or MIN value of the type is returned instead if the value is beyond the allowed window. For instance, toInt8(255)-> 127 and toInt8(-255)-> -128

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToInt

func ToInt[C IConvertable](i C) int

ToInt attempts to convert any IConvertable value to an int. If the conversion results in a value outside the range of an int, the closest boundary value will be returned.

func ToInt16

func ToInt16[C IConvertable](i C) int16

ToInt16 attempts to convert any IConvertable value to an int16. If the conversion results in a value outside the range of an int16, the closest boundary value will be returned.

func ToInt32

func ToInt32[C IConvertable](i C) int32

ToInt32 attempts to convert any IConvertable value to an int32. If the conversion results in a value outside the range of an int32, the closest boundary value will be returned.

func ToInt64

func ToInt64[C IConvertable](i C) int64

ToInt64 attempts to convert any IConvertable value to an int64. If the conversion results in a value outside the range of an int64, the closest boundary value will be returned.

func ToInt8

func ToInt8[C IConvertable](i C) int8

ToInt8 attempts to convert any IConvertable value to an int8. If the conversion results in a value outside the range of an int8, the closest boundary value will be returned.

func ToUint

func ToUint[C IConvertable](i C) uint

ToUint attempts to convert any IConvertable value to an uint. If the conversion results in a value outside the range of an uint, the closest boundary value will be returned.

func ToUint16

func ToUint16[C IConvertable](i C) uint16

ToUint16 attempts to convert any IConvertable value to an uint16. If the conversion results in a value outside the range of an uint16, the closest boundary value will be returned.

func ToUint32

func ToUint32[C IConvertable](i C) uint32

ToUint32 attempts to convert any IConvertable value to an uint32. If the conversion results in a value outside the range of an uint32, the closest boundary value will be returned.

func ToUint64

func ToUint64[C IConvertable](i C) uint64

ToUint64 attempts to convert any IConvertable value to an uint64. If the conversion results in a value outside the range of an uint64, the closest boundary value will be returned.

func ToUint8

func ToUint8[C IConvertable](i C) uint8

ToUint8 attempts to convert any IConvertable value to an uint8. If the conversion results in a value outside the range of an uint8, the closest boundary value will be returned.

Types

type IConvertable

type IConvertable interface {
	INumber
}

IConvertable is an alias for everything that can be converted

type IFloat

type IFloat interface {
	~float32 | ~float64
}

IFloat is an alias for the float32 and float64 types.

type IInteger

type IInteger interface {
	ISignedInteger | IUnsignedInteger
}

IInteger is an alias for the all unsigned and signed integers

type INumber

type INumber interface {
	IInteger | IFloat
}

INumber is an alias for all integers and floats

type ISignedInteger

type ISignedInteger interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

ISignedInteger is an alias for all signed integers: int, int8, int16, int32, and int64 types.

type IUnsignedInteger

type IUnsignedInteger interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

IUnsignedInteger is an alias for all unsigned integers: uint, uint8, uint16, uint32, and uint64 types.

Jump to

Keyboard shortcuts

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