safecast

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 3 Imported by: 61

README

🪄 go-safecast: safe numbers conversion

Go Report Card GoDoc codecov Code Climate

Project purpose

In Go, integer type conversion can lead to a silent and unexpected behavior and errors if not handled carefully.

This package is made to help to convert any number to another, and report an error when if there would be a loss or overflow in the conversion

Usage

package main

import (
  "fmt"
  "math"

  "github.com/ccoveille/go-safecast"
)

func main() {

  // when there is no overflow
  //
  fmt.Println(safecast.ToInt8(float64(42)))
  // Output: 42, nil
  fmt.Println(safecast.ToInt8(int64(-1)))
  // Output: -1, nil

  // when there is an overflow
  //
  fmt.Println(safecast.ToInt8(float64(20000)))
  // Output: 0 conversion issue: 20000 is greater than 127
  fmt.Println(safecast.ToUint8(int64(-1)))
  // Output: 0 conversion issue: -1 is negative
  fmt.Println(safecast.ToInt16(int32(40000)))
  // Output: 0 conversion issue: 40000 is greater than 32767
  fmt.Println(safecast.ToUint16(int64(-1)))
  // Output: 0 conversion issue: -1 is negative
  fmt.Println(safecast.ToInt32(math.MaxUint32 + 1))
  // Output: 0 conversion issue: 4294967296 is greater than 2147483647
  fmt.Println(safecast.ToUint32(int64(-1)))
  // Output: 0 conversion issue: -1 is negative
  fmt.Println(safecast.ToInt64(uint64(math.MaxInt64) + 1))
  // Output: 0 conversion issue: 9223372036854775808 is greater than 9223372036854775807
  fmt.Println(safecast.ToUint64(int8(-1)))
  // Output: 0 conversion issue: -1 is negative
  fmt.Println(safecast.ToInt(uint64(math.MaxInt) + 1))
  // Output: 0 conversion issue: 9223372036854775808 is greater than 9223372036854775807
  fmt.Println(safecast.ToUint(int8(-1)))
  // Output: 0 conversion issue: -1 is negative
}

Go Playground

Conversion overflows

Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.

package main

import "fmt"

func main() {
  var a int64
  a = 42
  b := uint8(a)
  fmt.Println(b) // 42

  a = 255 // this is the math.MaxUint8
  b = uint8(a)
  fmt.Println(b) // 255

  a = 255 + 1
  b = uint8(a)
  fmt.Println(b) // 0 conversion overflow

  a = -1
  b = uint8(a)
  fmt.Println(b) // 255 conversion overflow
}

Go Playground

Motivation

The gosec project raised this to my attention when the gosec G115 rule was added

G115: Potential overflow when converting between integer types.

This issue was way more complex than expected, and required multiple fixes.

CWE-190 explains in detail.

But to sum it up, you can face:

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

The gosec G115 will now report issues in a lot of project.

Alternatives

Some libraries existed, but they were not able to cover all the use cases.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConversionIssue = errors.New("conversion issue")

Functions

func ToInt

func ToInt[T Type](i T) (int, error)

ToInt attempts to convert any Type value to an int. If the conversion results in a value outside the range of an int, an ErrConversionOverflow error is returned.

func ToInt8

func ToInt8[T Type](i T) (int8, error)

ToInt8 attempts to convert any Type value to an int8. If the conversion results in a value outside the range of an int8, an ErrConversionOverflow error is returned.

func ToInt16

func ToInt16[T Type](i T) (int16, error)

ToInt16 attempts to convert any Type value to an int16. If the conversion results in a value outside the range of an int16, an ErrConversionOverflow error is returned.

func ToInt32

func ToInt32[T Type](i T) (int32, error)

ToInt32 attempts to convert any Type value to an int32. If the conversion results in a value outside the range of an int32, an ErrConversionOverflow error is returned.

func ToInt64

func ToInt64[T Type](i T) (int64, error)

ToInt64 attempts to convert any Type value to an int64. If the conversion results in a value outside the range of an int64, an ErrConversionOverflow error is returned.

func ToUint

func ToUint[T Type](i T) (uint, error)

ToUint attempts to convert any Type value to an uint. If the conversion results in a value outside the range of an uint, an ErrConversionOverflow error is returned.

func ToUint8

func ToUint8[T Type](i T) (uint8, error)

ToUint8 attempts to convert any Type value to an uint8. If the conversion results in a value outside the range of an uint8, an ErrConversionOverflow error is returned.

func ToUint16

func ToUint16[T Type](i T) (uint16, error)

ToUint16 attempts to convert any Type value to an uint16. If the conversion results in a value outside the range of an uint16, an ErrConversionOverflow error is returned.

func ToUint32

func ToUint32[T Type](i T) (uint32, error)

ToUint32 attempts to convert any Type value to an uint32. If the conversion results in a value outside the range of an uint32, an ErrConversionOverflow error is returned.

func ToUint64

func ToUint64[T Type](i T) (uint64, error)

ToUint64 attempts to convert any Type value to an uint64. If the conversion results in a value outside the range of an uint64, an ErrConversionOverflow error is returned.

Types

type Float added in v1.1.0

type Float interface {
	~float32 | ~float64
}

Float is an alias for the float32 and float64 types.

type Integer added in v1.1.0

type Integer interface {
	Signed | Unsigned
}

Integer is an alias for the all unsigned and signed integers

type Number

type Number interface {
	Integer | Float
}

Number is an alias for all integers and floats TODO: consider using complex, but not sure there is a need

type Signed added in v1.1.0

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

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

type Type added in v1.1.0

type Type interface {
	Number
}

Type is an alias for everything that can be converted

type Unsigned added in v1.1.0

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

Unsigned is an alias for all unsigned integers: uint, uint8, uint16, uint32, and uint64 types. TODO: support uintpr

Jump to

Keyboard shortcuts

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