From f7530845525cfdf79e5c59a6b043ebf8351494f9 Mon Sep 17 00:00:00 2001 From: illia-li Date: Tue, 8 Oct 2024 22:09:41 -0400 Subject: [PATCH] add error on `nil reference` values `counter` --- marshal/counter/unmarshal.go | 2 +- marshal/counter/unmarshal_utils.go | 108 ++++++++++++++++++++++++----- 2 files changed, 93 insertions(+), 17 deletions(-) diff --git a/marshal/counter/unmarshal.go b/marshal/counter/unmarshal.go index 62fdbfe8b..736a41763 100644 --- a/marshal/counter/unmarshal.go +++ b/marshal/counter/unmarshal.go @@ -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) diff --git a/marshal/counter/unmarshal_utils.go b/marshal/counter/unmarshal_utils.go index ca95b5ffc..d4cb2456c 100644 --- a/marshal/counter/unmarshal_utils.go +++ b/marshal/counter/unmarshal_utils.go @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 { @@ -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 { @@ -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) @@ -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 { @@ -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() { @@ -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()) } } @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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() { @@ -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()) } } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)