Browse Source

Error on unsigned conversions of negative values

tags/v1.0.0
Cameron Moore Bjørn Erik Pedersen 7 years ago
parent
commit
758cd85768
2 changed files with 145 additions and 0 deletions
  1. +40
    -0
      cast_test.go
  2. +105
    -0
      caste.go

+ 40
- 0
cast_test.go View File

@@ -37,6 +37,14 @@ func TestToUintE(t *testing.T) {
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
@@ -82,6 +90,14 @@ func TestToUint64E(t *testing.T) {
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
@@ -126,6 +142,14 @@ func TestToUint32E(t *testing.T) {
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
// errors
{"test", 0, true},
{testing.T{}, 0, true},
@@ -172,6 +196,14 @@ func TestToUint16E(t *testing.T) {
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
@@ -217,6 +249,14 @@ func TestToUint8E(t *testing.T) {
{"8", 8, false},
{nil, 0, false},
// errors
{int(-8), 0, true},
{int8(-8), 0, true},
{int16(-8), 0, true},
{int32(-8), 0, true},
{int64(-8), 0, true},
{float32(-8.31), 0, true},
{float64(-8.31), 0, true},
{"-8", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}


+ 105
- 0
caste.go View File

@@ -424,14 +424,29 @@ func ToUintE(i interface{}) (uint, error) {
}
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
case int:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case int64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case int32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case int16:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case int8:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case uint:
return s, nil
@@ -444,8 +459,14 @@ func ToUintE(i interface{}) (uint, error) {
case uint8:
return uint(s), nil
case float64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case float32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint")
}
return uint(s), nil
case bool:
if s {
@@ -471,14 +492,29 @@ func ToUint64E(i interface{}) (uint64, error) {
}
return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
case int:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case int64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case int32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case int16:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case int8:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case uint:
return uint64(s), nil
@@ -491,8 +527,14 @@ func ToUint64E(i interface{}) (uint64, error) {
case uint8:
return uint64(s), nil
case float32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case float64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint64")
}
return uint64(s), nil
case bool:
if s {
@@ -518,14 +560,29 @@ func ToUint32E(i interface{}) (uint32, error) {
}
return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
case int:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case int64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case int32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case int16:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case int8:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case uint:
return uint32(s), nil
@@ -538,8 +595,14 @@ func ToUint32E(i interface{}) (uint32, error) {
case uint8:
return uint32(s), nil
case float64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case float32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint32")
}
return uint32(s), nil
case bool:
if s {
@@ -565,14 +628,29 @@ func ToUint16E(i interface{}) (uint16, error) {
}
return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
case int:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case int64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case int32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case int16:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case int8:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case uint:
return uint16(s), nil
@@ -585,8 +663,14 @@ func ToUint16E(i interface{}) (uint16, error) {
case uint8:
return uint16(s), nil
case float64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case float32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint16")
}
return uint16(s), nil
case bool:
if s {
@@ -612,14 +696,29 @@ func ToUint8E(i interface{}) (uint8, error) {
}
return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
case int:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case int64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case int32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case int16:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case int8:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case uint:
return uint8(s), nil
@@ -632,8 +731,14 @@ func ToUint8E(i interface{}) (uint8, error) {
case uint8:
return s, nil
case float64:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case float32:
if s < 0 {
return 0, fmt.Errorf("unable to cast negative value to uint8")
}
return uint8(s), nil
case bool:
if s {


Loading…
Cancel
Save