safecast

package module
v1.0.0 Latest Latest
Warning

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

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

README

Go SafeCast

Origin of this project

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

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

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

G115: Potential integer overflow when converting between integer types

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

Example

This code seems OK

package main

import (
  "fmt"
)

func main() {
  var a uint64
  a = 42
  b := int32(a)
  fmt.Println(b) // 42
}

But the conversion to int32 will behave differently depending on the value

package main

import (
  "fmt"
)

func main() {
  var a uint64
  a = 2147483647
  b := int32(a)
  fmt.Println(b) // 2147483647

  a = 2147483647 + 1
  b = int32(a)
  fmt.Println(b) // -2147483648 integer overflow

  c := -1
  d := uint32(c)
  fmt.Println(d) // 4294967295
}

GoPlay: https://go.dev/play/p/9PRWI7e0x1T

What is the problem ?

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

Motivation

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 ErrOutOfRange = errors.New("out of range")

Functions

func ToInt

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

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

func ToInt8

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

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

func ToInt16

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

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

func ToInt32

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

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

func ToInt64

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

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

func ToUint

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

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

func ToUint8

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

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

func ToUint16

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

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

func ToUint32

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

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

func ToUint64

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

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

Types

type Number

type Number interface {
	~int | ~uint | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64
}

Number is an alias for the int, uint, int8, uint8, int16, uint16, int32, uint32, int64, and uint64 types.

Jump to

Keyboard shortcuts

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