From ad9ac6d07ce1819260c2b7f090fd4fe742c80d9f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 10 Jul 2022 23:49:07 -0600 Subject: Fuzz btrfsitem, and by consequence improve binstruct errors --- lib/binstruct/marshal.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'lib/binstruct/marshal.go') diff --git a/lib/binstruct/marshal.go b/lib/binstruct/marshal.go index 8159191..78a4bb5 100644 --- a/lib/binstruct/marshal.go +++ b/lib/binstruct/marshal.go @@ -14,7 +14,15 @@ type Marshaler = encoding.BinaryMarshaler func Marshal(obj any) ([]byte, error) { if mar, ok := obj.(Marshaler); ok { - return mar.MarshalBinary() + dat, err := mar.MarshalBinary() + if err != nil { + err = &UnmarshalError{ + Type: reflect.TypeOf(obj), + Method: "MarshalBinary", + Err: err, + } + } + return dat, err } return MarshalWithoutInterface(obj) } @@ -24,7 +32,15 @@ func MarshalWithoutInterface(obj any) ([]byte, error) { switch val.Kind() { case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64: typ := intKind2Type[val.Kind()] - return val.Convert(typ).Interface().(Marshaler).MarshalBinary() + dat, err := val.Convert(typ).Interface().(Marshaler).MarshalBinary() + if err != nil { + err = &UnmarshalError{ + Type: typ, + Method: "MarshalBinary", + Err: err, + } + } + return dat, err case reflect.Ptr: return Marshal(val.Elem().Interface()) case reflect.Array: @@ -40,7 +56,10 @@ func MarshalWithoutInterface(obj any) ([]byte, error) { case reflect.Struct: return getStructHandler(val.Type()).Marshal(val) default: - panic(fmt.Errorf("type=%v does not implement binfmt.Marshaler and kind=%v is not a supported statically-sized kind", - val.Type(), val.Kind())) + panic(&InvalidTypeError{ + Type: val.Type(), + Err: fmt.Errorf("does not implement binfmt.Marshaler and kind=%v is not a supported statically-sized kind", + val.Kind()), + }) } } -- cgit v1.2.3-2-g168b