safecast

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 2 Imported by: 4

README

go-safecast

Library for safe type conversion in Go

What is this

The type of int equals int64 on 64-bit machine in Go.
When you convert int(int64) to int32, int8 or int6, Your code could have Integer Overflow vulnerability.

In 2019, Kubernetes had the vulnerability. and the vulnerability was found on Security Audit Project by Trail of Bits.

You can use this library to prevent the vulnerability creation.

(This library is inspired by Kubernetes's Security Audit Report by Trail of Bits)

Usage

import "github.com/rung/go-safecast"

Convert int to int32 (instead of native int32() type conversion)

	i := 2147483647
	i32, err := safecast.Int32(i) // convert int to int32 in a safe way
	if err != nil {
		return err
	}

This library also has safecast.Int16 and safecast.Int8. You can use the functions in the same way as safecast.Int32

Convert string to int32 (instead of strconv.Atoi())

	s := "2147483647"
	i, err := safecast.Atoi32(s) // convert string to int32 in a safe way
	if err != nil {
		return err
	}

This library also has safecast.Atoi16 and safecast.Atoi8. You can use the functions in the same way as safecast.Atoi32

What happens when overflows

Range of each integer

int32 (32bit signed integer) int16 (16bit signed integer) int8 (8bit signed integer)
Range From -2,147,483,648 to 2,147,483,647 From -32,768 to 32,767 From -128 to 127

When using native int32(), the code causes overflows

Link: Go Playground


When using safecast.Int32() on this library, your code is safe

This library can detect integer overflow. so you can convert integer in a safe way.

Link: Go Playground

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Atoi16

func Atoi16(s string) (int16, error)

Atoi16 converts string to int16 in a safe way. You get error when the value is out of the 16-bit range.

This is a wrapper function of strconv.ParseInt.

func Atoi32

func Atoi32(s string) (int32, error)

Atoi32 converts string to int32 in a safe way. You get error when the value is out of the 32-bit range.

This is a wrapper function of strconv.ParseInt.

func Atoi8

func Atoi8(s string) (int8, error)

Atoi8 converts string to int8 in a safe way. You get error when the value is out of the 8-bit range.

This is a wrapper function of strconv.ParseInt.

func Int16

func Int16(i int) (int16, error)

Int16 converts int to int16 in a safe way. You get error when the value is out of the 16-bit range.

func Int32

func Int32(i int) (int32, error)

Int32 converts int to int32 in a safe way. You get error when the value is out of the 32-bit range.

func Int8

func Int8(i int) (int8, error)

Int8 converts int to int8 in a safe way. You get error when the value is out of the 8-bit range.

Types

This section is empty.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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