diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-10 23:49:07 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-11 00:44:30 -0600 |
commit | ad9ac6d07ce1819260c2b7f090fd4fe742c80d9f (patch) | |
tree | ab6a607ea8575382c978f07de943ccf6c077de7c /lib/binstruct/marshal.go | |
parent | 41ef03aabf8d6db4f926480fc5ddfec014e342d3 (diff) |
Fuzz btrfsitem, and by consequence improve binstruct errors
Diffstat (limited to 'lib/binstruct/marshal.go')
-rw-r--r-- | lib/binstruct/marshal.go | 27 |
1 files changed, 23 insertions, 4 deletions
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()), + }) } } |