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/size.go | |
| parent | 41ef03aabf8d6db4f926480fc5ddfec014e342d3 (diff) | |
Fuzz btrfsitem, and by consequence improve binstruct errors
Diffstat (limited to 'lib/binstruct/size.go')
| -rw-r--r-- | lib/binstruct/size.go | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go index 03b42d8..365da85 100644 --- a/lib/binstruct/size.go +++ b/lib/binstruct/size.go @@ -5,6 +5,7 @@ package binstruct import ( + "errors" "fmt" "reflect" ) @@ -31,6 +32,14 @@ func staticSize(typ reflect.Type) (int, error) { if typ.Implements(staticSizerType) { return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize(), nil } + if typ.Implements(marshalerType) || typ.Implements(unmarshalerType) { + // If you implement binstruct.Marshaler or binstruct.Unmarshaler, + // then you must implement if you wish to be statically sized. + return 0, &InvalidTypeError{ + Type: typ, + Err: errors.New("does not implement binstruct.StaticSizer but does implement binstruct.Marshaler or binstruct.Unmarshaler"), + } + } switch typ.Kind() { case reflect.Uint8, reflect.Int8: return 1, nil @@ -49,13 +58,12 @@ func staticSize(typ reflect.Type) (int, error) { } return elemSize * typ.Len(), nil case reflect.Struct: - if !(typ.Implements(marshalerType) || typ.Implements(unmarshalerType)) { - return getStructHandler(typ).Size, nil - } - return 0, fmt.Errorf("type=%v (kind=%v) does not implement binfmt.StaticSizer but does implement binfmt.Marshaler or binfmt.Unmarshaler", - typ, typ.Kind()) + return getStructHandler(typ).Size, nil default: - return 0, fmt.Errorf("type=%v does not implement binfmt.StaticSizer and kind=%v is not a supported statically-sized kind", - typ, typ.Kind()) + return 0, &InvalidTypeError{ + Type: typ, + Err: fmt.Errorf("does not implement binfmt.StaticSizer and kind=%v is not a supported statically-sized kind", + typ.Kind()), + } } } |
