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/size.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'lib/binstruct/size.go') 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()), + } } } -- cgit v1.2.3-2-g168b