Skip to content

Commit

Permalink
fix: float overflow when converted to integer
Browse files Browse the repository at this point in the history
Only uint64 and uint was working as expected

Fixes #23
  • Loading branch information
ccoVeille committed Sep 8, 2024
1 parent 143314c commit a4b2e46
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 47 deletions.
46 changes: 45 additions & 1 deletion asserters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,51 @@ import "fmt"

func assertNotNegative[T Type](i T) error {
if i < 0 {
return fmt.Errorf("%w: %v is negative", ErrOutOfRange, i)
return fmt.Errorf("%w: %v is negative", ErrIntegerOverflow, i)
}
return nil
}

func checkUpperBoundary[T Type](value T, boundary uint64) error {
if value <= 0 {
return nil
}

var bigger bool
switch f := any(value).(type) {
case float32:
bigger = float64(f) >= float64(boundary)
case float64:
bigger = float64(f) >= float64(boundary)
default:
bigger = uint64(value) > boundary
}

if bigger {
return fmt.Errorf("%w: %v is greater than %v", ErrIntegerOverflow, value, boundary)
}

return nil
}

func checkLowerBoundary[T Type](value T, boundary int64) error {
if value >= 0 {
return nil
}

var smaller bool
switch f := any(value).(type) {
case float32:
smaller = float64(f) <= float64(boundary)
case float64:
smaller = float64(f) <= float64(boundary)
default:
smaller = int64(value) < boundary
}

if smaller {
return fmt.Errorf("%w: %v is lower than %v", ErrIntegerOverflow, value, boundary)
}

return nil
}
81 changes: 41 additions & 40 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,148 +4,149 @@

package safecast

import (
"fmt"
"math"
)
import "math"

// ToInt attempts to convert any [Type] value to an int.
// If the conversion results in a value outside the range of an int,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToInt[T Type](i T) (int, error) {
if i > 0 && uint64(i) > math.MaxInt {
return 0, fmt.Errorf("%w: %v is greater than math.MaxInt", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxInt); err != nil {
return 0, err
}

if err := checkLowerBoundary(i, math.MinInt); err != nil {
return 0, err
}

return int(i), nil
}

// ToUint attempts to convert any [Type] value to an uint.
// If the conversion results in a value outside the range of an uint,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToUint[T Type](i T) (uint, error) {
if err := assertNotNegative(i); err != nil {
return 0, err
}

if float64(i) > math.MaxUint64 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxUint64", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxUint64); err != nil {
return 0, err
}

return uint(i), nil
}

// ToInt8 attempts to convert any [Type] value to an int8.
// If the conversion results in a value outside the range of an int8,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToInt8[T Type](i T) (int8, error) {
if i > math.MaxInt8 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxInt8", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxInt8); err != nil {
return 0, err
}

if i < 0 && uint64(-i) > -math.MinInt8 {
return 0, fmt.Errorf("%w: %v is less than math.MinInt8", ErrOutOfRange, i)
if err := checkLowerBoundary(i, math.MinInt8); err != nil {
return 0, err
}

return int8(i), nil
}

// ToUint8 attempts to convert any [Type] value to an uint8.
// If the conversion results in a value outside the range of an uint8,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToUint8[T Type](i T) (uint8, error) {
if err := assertNotNegative(i); err != nil {
return 0, err
}

if uint64(i) > math.MaxUint8 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxUint8", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxUint8); err != nil {
return 0, err
}

return uint8(i), nil
}

// ToInt16 attempts to convert any [Type] value to an int16.
// If the conversion results in a value outside the range of an int16,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToInt16[T Type](i T) (int16, error) {
if i > 0 && uint64(i) > math.MaxInt16 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxInt16", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxInt16); err != nil {
return 0, err
}

if i < 0 && uint64(-i) > -math.MinInt16 {
return 0, fmt.Errorf("%w: %v is less than math.MinInt16", ErrOutOfRange, i)
if err := checkLowerBoundary(i, math.MinInt16); err != nil {
return 0, err
}

return int16(i), nil
}

// ToUint16 attempts to convert any [Type] value to an uint16.
// If the conversion results in a value outside the range of an uint16,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToUint16[T Type](i T) (uint16, error) {
if err := assertNotNegative(i); err != nil {
return 0, err
}

if uint64(i) > math.MaxUint16 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxUint16", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxUint16); err != nil {
return 0, err
}

return uint16(i), nil
}

// ToInt32 attempts to convert any [Type] value to an int32.
// If the conversion results in a value outside the range of an int32,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToInt32[T Type](i T) (int32, error) {
if i > 0 && uint64(i) > math.MaxInt32 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxInt32", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxInt32); err != nil {
return 0, err
}

if i < 0 && uint64(-i) > -math.MinInt32 {
return 0, fmt.Errorf("%w: %v is less than math.MinInt32", ErrOutOfRange, i)
if err := checkLowerBoundary(i, math.MinInt32); err != nil {
return 0, err
}

return int32(i), nil
}

// ToUint32 attempts to convert any [Type] value to an uint32.
// If the conversion results in a value outside the range of an uint32,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToUint32[T Type](i T) (uint32, error) {
if err := assertNotNegative(i); err != nil {
return 0, err
}

if uint64(i) > math.MaxUint32 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxUint32", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxUint32); err != nil {
return 0, err
}

return uint32(i), nil
}

// ToInt64 attempts to convert any [Type] value to an int64.
// If the conversion results in a value outside the range of an int64,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToInt64[T Type](i T) (int64, error) {
if i > 0 && uint64(i) > math.MaxInt64 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxInt64", ErrOutOfRange, i)
if err := checkUpperBoundary(i, math.MaxInt64); err != nil {
return 0, err
}

return int64(i), nil
}

// ToUint64 attempts to convert any [Type] value to an uint64.
// If the conversion results in a value outside the range of an uint64,
// an ErrOutOfRange error is returned.
// an ErrIntegerOverflow error is returned.
func ToUint64[T Type](i T) (uint64, error) {
if err := assertNotNegative(i); err != nil {
return 0, err
}

if float64(i) > math.MaxUint64 {
return 0, fmt.Errorf("%w: %v is greater than math.MaxUint64", ErrOutOfRange, i)
if err := checkUpperBoundary(i, uint64(math.MaxUint64)); err != nil {
return 0, err
}

return uint64(i), nil
Expand Down
20 changes: 15 additions & 5 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func requireError(t *testing.T, err error) {
t.Fatal("expected error")
}

if !errors.Is(err, safecast.ErrOutOfRange) {
if !errors.Is(err, safecast.ErrIntegerOverflow) {
t.Errorf("expected out of range error, got %v", err)
}
}
Expand Down Expand Up @@ -1272,6 +1272,8 @@ func TestToUint64(t *testing.T) {
})

assertUint64Error(t, []caseUint64[float32]{
{name: "negative value", input: -1},
{name: "out of range max uint64", input: math.MaxUint64},
{name: "out of range max float32", input: math.MaxFloat32},
})
})
Expand All @@ -1284,6 +1286,8 @@ func TestToUint64(t *testing.T) {
})

assertUint64Error(t, []caseUint64[float64]{
{name: "negative value", input: -1},
{name: "out of range max uint64", input: math.MaxUint64},
{name: "out of range max float32", input: math.MaxFloat32},
{name: "out of range max float64", input: math.MaxFloat64},
})
Expand Down Expand Up @@ -1412,7 +1416,8 @@ func TestToInt(t *testing.T) {
})

assertIntError(t, []caseInt[float32]{
{name: "positive out of range", input: math.MaxFloat32 + 1},
{name: "positive out of range", input: math.MaxFloat32},
{name: "negative out of range", input: -math.MaxFloat32},
})
})

Expand All @@ -1423,8 +1428,9 @@ func TestToInt(t *testing.T) {
{name: "positive within range", input: 10000.9, want: 10000},
})

assertIntError(t, []caseInt[float32]{
{name: "positive out of range", input: math.MaxFloat32 + 1},
assertIntError(t, []caseInt[float64]{
{name: "positive out of range", input: math.MaxFloat32},
{name: "negative out of range", input: -math.MaxFloat32},
})
})
}
Expand Down Expand Up @@ -1558,7 +1564,9 @@ func TestToUint(t *testing.T) {
})

assertUint64Error(t, []caseUint64[float32]{
{name: "out of range", input: math.MaxFloat32},
{name: "negative value", input: -1},
{name: "out of range max uint64", input: math.MaxUint64},
{name: "out of range max float32", input: math.MaxFloat32},
})
})

Expand All @@ -1570,6 +1578,8 @@ func TestToUint(t *testing.T) {
})

assertUintError(t, []caseUint[float64]{
{name: "negative value", input: -1},
{name: "out of range max uint64", input: math.MaxUint64},
{name: "out of range max float32", input: math.MaxFloat32},
{name: "out of range max float64", input: math.MaxFloat64},
})
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package safecast

import "errors"

var ErrOutOfRange = errors.New("out of range")
var ErrIntegerOverflow = errors.New("integer overflow")

0 comments on commit a4b2e46

Please sign in to comment.