Skip to content

Commit

Permalink
add error on nil reference values counter
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Oct 9, 2024
1 parent 4d01de9 commit f753084
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 17 deletions.
2 changes: 1 addition & 1 deletion marshal/counter/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Unmarshal(data []byte, value interface{}) error {
rv := reflect.ValueOf(value)
rt := rv.Type()
if rt.Kind() != reflect.Ptr {
return fmt.Errorf("failed to unmarshal counter: unsupported value type (%T)(%#[1]v)", value)
return fmt.Errorf("failed to unmarshal counter: unsupported value type %#v", value)
}
if rt.Elem().Kind() != reflect.Ptr {
return DecReflect(data, rv)
Expand Down
108 changes: 92 additions & 16 deletions marshal/counter/unmarshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import (

var errWrongDataLen = fmt.Errorf("failed to unmarshal counter: the length of the data should be 0 or 8")

func errNilReference(v interface{}) error {
return fmt.Errorf("failed to unmarshal counter: can not unmarshal into nil reference %#v)", v)
}

func DecInt8(p []byte, v *int8) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -27,6 +34,9 @@ func DecInt8(p []byte, v *int8) error {
}

func DecInt8R(p []byte, v **int8) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -48,6 +58,9 @@ func DecInt8R(p []byte, v **int8) error {
}

func DecInt16(p []byte, v *int16) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -64,6 +77,9 @@ func DecInt16(p []byte, v *int16) error {
}

func DecInt16R(p []byte, v **int16) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -85,6 +101,9 @@ func DecInt16R(p []byte, v **int16) error {
}

func DecInt32(p []byte, v *int32) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -101,6 +120,9 @@ func DecInt32(p []byte, v *int32) error {
}

func DecInt32R(p []byte, v **int32) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -122,6 +144,9 @@ func DecInt32R(p []byte, v **int32) error {
}

func DecInt64(p []byte, v *int64) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -134,6 +159,9 @@ func DecInt64(p []byte, v *int64) error {
}

func DecInt64R(p []byte, v **int64) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -151,6 +179,9 @@ func DecInt64R(p []byte, v **int64) error {
}

func DecInt(p []byte, v *int) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -163,6 +194,9 @@ func DecInt(p []byte, v *int) error {
}

func DecIntR(p []byte, v **int) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -180,6 +214,9 @@ func DecIntR(p []byte, v **int) error {
}

func DecUint8(p []byte, v *uint8) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -195,6 +232,9 @@ func DecUint8(p []byte, v *uint8) error {
}

func DecUint8R(p []byte, v **uint8) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -215,6 +255,9 @@ func DecUint8R(p []byte, v **uint8) error {
}

func DecUint16(p []byte, v *uint16) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -230,6 +273,9 @@ func DecUint16(p []byte, v *uint16) error {
}

func DecUint16R(p []byte, v **uint16) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -250,6 +296,9 @@ func DecUint16R(p []byte, v **uint16) error {
}

func DecUint32(p []byte, v *uint32) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -265,6 +314,9 @@ func DecUint32(p []byte, v *uint32) error {
}

func DecUint32R(p []byte, v **uint32) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -285,6 +337,9 @@ func DecUint32R(p []byte, v **uint32) error {
}

func DecUint64(p []byte, v *uint64) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -297,6 +352,9 @@ func DecUint64(p []byte, v *uint64) error {
}

func DecUint64R(p []byte, v **uint64) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -314,6 +372,9 @@ func DecUint64R(p []byte, v **uint64) error {
}

func DecUint(p []byte, v *uint) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
*v = 0
Expand All @@ -326,6 +387,9 @@ func DecUint(p []byte, v *uint) error {
}

func DecUintR(p []byte, v **uint) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -343,6 +407,9 @@ func DecUintR(p []byte, v **uint) error {
}

func DecString(p []byte, v *string) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -359,6 +426,9 @@ func DecString(p []byte, v *string) error {
}

func DecStringR(p []byte, v **string) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -377,6 +447,9 @@ func DecStringR(p []byte, v **string) error {
}

func DecBigInt(p []byte, v *big.Int) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
v.SetInt64(0)
Expand All @@ -389,6 +462,9 @@ func DecBigInt(p []byte, v *big.Int) error {
}

func DecBigIntR(p []byte, v **big.Int) error {
if v == nil {
return errNilReference(v)
}
switch len(p) {
case 0:
if p == nil {
Expand All @@ -406,7 +482,7 @@ func DecBigIntR(p []byte, v **big.Int) error {

func DecReflect(p []byte, v reflect.Value) error {
if v.IsNil() {
return fmt.Errorf("failed to unmarshal counter: can not unmarshal into nil reference (%T)(%#[1]v)", v.Interface())
return fmt.Errorf("failed to unmarshal counter: can not unmarshal into nil reference %#v", v.Interface())
}

switch v = v.Elem(); v.Kind() {
Expand All @@ -429,7 +505,7 @@ func DecReflect(p []byte, v reflect.Value) error {
case reflect.String:
return decReflectString(p, v)
default:
return fmt.Errorf("failed to unmarshal counter: unsupported value type (%T)(%#[1]v)", v.Interface())
return fmt.Errorf("failed to unmarshal counter: unsupported value type %#v", v.Interface())
}
}

Expand All @@ -440,7 +516,7 @@ func decReflectInt8(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt8 || val < math.MinInt8 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int8, the data should be in the int8 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int8 range", v.Interface())
}
v.SetInt(val)
default:
Expand All @@ -456,7 +532,7 @@ func decReflectInt16(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt16 || val < math.MinInt16 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int16, the data should be in the int16 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int16 range", v.Interface())
}
v.SetInt(val)
default:
Expand All @@ -472,7 +548,7 @@ func decReflectInt32(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt32 || val < math.MinInt32 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int32, the data should be in the int32 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int32 range", v.Interface())
}
v.SetInt(val)
default:
Expand All @@ -499,7 +575,7 @@ func decReflectUint8(p []byte, v reflect.Value) error {
v.SetUint(0)
case 8:
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 || p[4] != 0 || p[5] != 0 || p[6] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint8, the data should be in the uint8 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint8 range", v.Interface())
}
v.SetUint(uint64(p[7]))
default:
Expand All @@ -514,7 +590,7 @@ func decReflectUint16(p []byte, v reflect.Value) error {
v.SetUint(0)
case 8:
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 || p[4] != 0 || p[5] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint16, the data should be in the uint16 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint16 range", v.Interface())
}
v.SetUint(uint64(p[6])<<8 | uint64(p[7]))
default:
Expand All @@ -529,7 +605,7 @@ func decReflectUint32(p []byte, v reflect.Value) error {
v.SetUint(0)
case 8:
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint32, the data should be in the uint32 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint32 range", v.Interface())
}
v.SetUint(uint64(p[4])<<24 | uint64(p[5])<<16 | uint64(p[6])<<8 | uint64(p[7]))
default:
Expand Down Expand Up @@ -568,7 +644,7 @@ func decReflectString(p []byte, v reflect.Value) error {

func DecReflectR(p []byte, v reflect.Value) error {
if v.IsNil() {
return fmt.Errorf("failed to unmarshal counter: can not unmarshal into nil reference (%T)(%#[1]v)", v.Interface())
return fmt.Errorf("failed to unmarshal counter: can not unmarshal into nil reference %#v", v.Interface())
}

switch v.Type().Elem().Elem().Kind() {
Expand All @@ -591,7 +667,7 @@ func DecReflectR(p []byte, v reflect.Value) error {
case reflect.String:
return decReflectStringR(p, v)
default:
return fmt.Errorf("failed to unmarshal counter: unsupported value type (%T)(%#[1]v)", v.Interface())
return fmt.Errorf("failed to unmarshal counter: unsupported value type %#v", v.Interface())
}
}

Expand All @@ -602,7 +678,7 @@ func decReflectInt8R(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt8 || val < math.MinInt8 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int8, the data should be in the int8 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int8 range", v.Interface())
}
newVal := reflect.New(v.Type().Elem().Elem())
newVal.Elem().SetInt(val)
Expand All @@ -620,7 +696,7 @@ func decReflectInt16R(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt16 || val < math.MinInt16 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int16, the data should be in the int16 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int16 range", v.Interface())
}
newVal := reflect.New(v.Type().Elem().Elem())
newVal.Elem().SetInt(val)
Expand All @@ -638,7 +714,7 @@ func decReflectInt32R(p []byte, v reflect.Value) error {
case 8:
val := decInt64(p)
if val > math.MaxInt32 || val < math.MinInt32 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom int32, the data should be in the int32 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the int32 range", v.Interface())
}
newVal := reflect.New(v.Type().Elem().Elem())
newVal.Elem().SetInt(val)
Expand Down Expand Up @@ -670,7 +746,7 @@ func decReflectUint8R(p []byte, v reflect.Value) error {
case 8:
newVal := reflect.New(v.Type().Elem().Elem())
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 || p[4] != 0 || p[5] != 0 || p[6] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint8, the data should be in the uint8 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint8 range", v.Interface())
}
newVal.Elem().SetUint(uint64(p[7]))
v.Elem().Set(newVal)
Expand All @@ -687,7 +763,7 @@ func decReflectUint16R(p []byte, v reflect.Value) error {
case 8:
newVal := reflect.New(v.Type().Elem().Elem())
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 || p[4] != 0 || p[5] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint16, the data should be in the uint16 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint16 range", v.Interface())
}
newVal.Elem().SetUint(uint64(p[6])<<8 | uint64(p[7]))
v.Elem().Set(newVal)
Expand All @@ -704,7 +780,7 @@ func decReflectUint32R(p []byte, v reflect.Value) error {
case 8:
newVal := reflect.New(v.Type().Elem().Elem())
if p[0] != 0 || p[1] != 0 || p[2] != 0 || p[3] != 0 {
return fmt.Errorf("failed to unmarshal counter: to unmarshal into custom uint32, the data should be in the uint32 range")
return fmt.Errorf("failed to unmarshal counter: to unmarshal into %T, the data should be in the uint32 range", v.Interface())
}
newVal.Elem().SetUint(uint64(p[4])<<24 | uint64(p[5])<<16 | uint64(p[6])<<8 | uint64(p[7]))
v.Elem().Set(newVal)
Expand Down

0 comments on commit f753084

Please sign in to comment.