summaryrefslogtreecommitdiff
path: root/lib/binstruct
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-10 23:49:07 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-11 00:44:30 -0600
commitad9ac6d07ce1819260c2b7f090fd4fe742c80d9f (patch)
treeab6a607ea8575382c978f07de943ccf6c077de7c /lib/binstruct
parent41ef03aabf8d6db4f926480fc5ddfec014e342d3 (diff)
Fuzz btrfsitem, and by consequence improve binstruct errors
Diffstat (limited to 'lib/binstruct')
-rw-r--r--lib/binstruct/binint/builtins.go38
-rw-r--r--lib/binstruct/binutil/binutil.go16
-rw-r--r--lib/binstruct/errors.go48
-rw-r--r--lib/binstruct/marshal.go27
-rw-r--r--lib/binstruct/size.go22
-rw-r--r--lib/binstruct/structs.go5
-rw-r--r--lib/binstruct/unmarshal.go23
7 files changed, 141 insertions, 38 deletions
diff --git a/lib/binstruct/binint/builtins.go b/lib/binstruct/binint/builtins.go
index 5363dbe..01186bc 100644
--- a/lib/binstruct/binint/builtins.go
+++ b/lib/binstruct/binint/builtins.go
@@ -6,15 +6,9 @@ package binint
import (
"encoding/binary"
- "fmt"
-)
-func needNBytes(t interface{}, dat []byte, n int) error {
- if len(dat) < n {
- return fmt.Errorf("%T.UnmarshalBinary: need at least %v bytes, only have %v", t, n, len(dat))
- }
- return nil
-}
+ "git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil"
+)
// unsigned
@@ -23,7 +17,7 @@ type U8 uint8
func (U8) BinaryStaticSize() int { return 1 }
func (x U8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil }
func (x *U8) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 1); err != nil {
+ if err := binutil.NeedNBytes(dat, 1); err != nil {
return 0, err
}
*x = U8(dat[0])
@@ -41,7 +35,7 @@ func (x U16le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U16le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 2); err != nil {
+ if err := binutil.NeedNBytes(dat, 2); err != nil {
return 0, err
}
*x = U16le(binary.LittleEndian.Uint16(dat))
@@ -57,7 +51,7 @@ func (x U32le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U32le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 4); err != nil {
+ if err := binutil.NeedNBytes(dat, 4); err != nil {
return 0, err
}
*x = U32le(binary.LittleEndian.Uint32(dat))
@@ -73,7 +67,7 @@ func (x U64le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U64le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 8); err != nil {
+ if err := binutil.NeedNBytes(dat, 8); err != nil {
return 0, err
}
*x = U64le(binary.LittleEndian.Uint64(dat))
@@ -91,7 +85,7 @@ func (x U16be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U16be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 2); err != nil {
+ if err := binutil.NeedNBytes(dat, 2); err != nil {
return 0, err
}
*x = U16be(binary.BigEndian.Uint16(dat))
@@ -107,7 +101,7 @@ func (x U32be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U32be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 4); err != nil {
+ if err := binutil.NeedNBytes(dat, 4); err != nil {
return 0, err
}
*x = U32be(binary.BigEndian.Uint32(dat))
@@ -123,7 +117,7 @@ func (x U64be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *U64be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 8); err != nil {
+ if err := binutil.NeedNBytes(dat, 8); err != nil {
return 0, err
}
*x = U64be(binary.BigEndian.Uint64(dat))
@@ -137,7 +131,7 @@ type I8 int8
func (I8) BinaryStaticSize() int { return 1 }
func (x I8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil }
func (x *I8) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 1); err != nil {
+ if err := binutil.NeedNBytes(dat, 1); err != nil {
return 0, err
}
*x = I8(dat[0])
@@ -155,7 +149,7 @@ func (x I16le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I16le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 2); err != nil {
+ if err := binutil.NeedNBytes(dat, 2); err != nil {
return 0, err
}
*x = I16le(binary.LittleEndian.Uint16(dat))
@@ -171,7 +165,7 @@ func (x I32le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I32le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 4); err != nil {
+ if err := binutil.NeedNBytes(dat, 4); err != nil {
return 0, err
}
*x = I32le(binary.LittleEndian.Uint32(dat))
@@ -187,7 +181,7 @@ func (x I64le) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I64le) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 8); err != nil {
+ if err := binutil.NeedNBytes(dat, 8); err != nil {
return 0, err
}
*x = I64le(binary.LittleEndian.Uint64(dat))
@@ -205,7 +199,7 @@ func (x I16be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I16be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 2); err != nil {
+ if err := binutil.NeedNBytes(dat, 2); err != nil {
return 0, err
}
*x = I16be(binary.BigEndian.Uint16(dat))
@@ -221,7 +215,7 @@ func (x I32be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I32be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 4); err != nil {
+ if err := binutil.NeedNBytes(dat, 4); err != nil {
return 0, err
}
*x = I32be(binary.BigEndian.Uint32(dat))
@@ -237,7 +231,7 @@ func (x I64be) MarshalBinary() ([]byte, error) {
return buf[:], nil
}
func (x *I64be) UnmarshalBinary(dat []byte) (int, error) {
- if err := needNBytes(*x, dat, 8); err != nil {
+ if err := binutil.NeedNBytes(dat, 8); err != nil {
return 0, err
}
*x = I64be(binary.BigEndian.Uint64(dat))
diff --git a/lib/binstruct/binutil/binutil.go b/lib/binstruct/binutil/binutil.go
new file mode 100644
index 0000000..684237f
--- /dev/null
+++ b/lib/binstruct/binutil/binutil.go
@@ -0,0 +1,16 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package binutil
+
+import (
+ "fmt"
+)
+
+func NeedNBytes(dat []byte, n int) error {
+ if len(dat) < n {
+ return fmt.Errorf("need at least %v bytes, only have %v", n, len(dat))
+ }
+ return nil
+}
diff --git a/lib/binstruct/errors.go b/lib/binstruct/errors.go
new file mode 100644
index 0000000..3914ec7
--- /dev/null
+++ b/lib/binstruct/errors.go
@@ -0,0 +1,48 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package binstruct
+
+import (
+ "fmt"
+ "reflect"
+)
+
+type InvalidTypeError struct {
+ Type reflect.Type
+ Err error
+}
+
+func (e *InvalidTypeError) Error() string {
+ return fmt.Sprintf("%v: %v", e.Type, e.Err)
+}
+func (e *InvalidTypeError) Unwrap() error { return e.Err }
+
+type UnmarshalError struct {
+ Type reflect.Type
+ Method string
+ Err error
+}
+
+func (e *UnmarshalError) Error() string {
+ if e.Method == "" {
+ return fmt.Sprintf("%v: %v", e.Type, e.Err)
+ }
+ return fmt.Sprintf("(%v).%v: %v", e.Type, e.Method, e.Err)
+}
+func (e *UnmarshalError) Unwrap() error { return e.Err }
+
+type MarshalError struct {
+ Type reflect.Type
+ Method string
+ Err error
+}
+
+func (e *MarshalError) Error() string {
+ if e.Method == "" {
+ return fmt.Sprintf("%v: %v", e.Type, e.Err)
+ }
+ return fmt.Sprintf("(%v).%v: %v", e.Type, e.Method, e.Err)
+}
+func (e *MarshalError) Unwrap() error { return e.Err }
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()),
+ })
}
}
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()),
+ }
}
}
diff --git a/lib/binstruct/structs.go b/lib/binstruct/structs.go
index 2f224dd..72fd5e5 100644
--- a/lib/binstruct/structs.go
+++ b/lib/binstruct/structs.go
@@ -183,7 +183,10 @@ func getStructHandler(typ reflect.Type) structHandler {
h, err := genStructHandler(typ)
if err != nil {
- panic(err)
+ panic(&InvalidTypeError{
+ Type: typ,
+ Err: err,
+ })
}
structCache[typ] = h
return h
diff --git a/lib/binstruct/unmarshal.go b/lib/binstruct/unmarshal.go
index c545137..61c2a4a 100644
--- a/lib/binstruct/unmarshal.go
+++ b/lib/binstruct/unmarshal.go
@@ -5,6 +5,7 @@
package binstruct
import (
+ "errors"
"fmt"
"reflect"
)
@@ -15,7 +16,15 @@ type Unmarshaler interface {
func Unmarshal(dat []byte, dstPtr any) (int, error) {
if unmar, ok := dstPtr.(Unmarshaler); ok {
- return unmar.UnmarshalBinary(dat)
+ n, err := unmar.UnmarshalBinary(dat)
+ if err != nil {
+ err = &UnmarshalError{
+ Type: reflect.TypeOf(dstPtr),
+ Method: "UnmarshalBinary",
+ Err: err,
+ }
+ }
+ return n, err
}
return UnmarshalWithoutInterface(dat, dstPtr)
}
@@ -23,7 +32,10 @@ func Unmarshal(dat []byte, dstPtr any) (int, error) {
func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) {
_dstPtr := reflect.ValueOf(dstPtr)
if _dstPtr.Kind() != reflect.Ptr {
- return 0, fmt.Errorf("not a pointer: %v", _dstPtr.Type())
+ panic(&InvalidTypeError{
+ Type: _dstPtr.Type(),
+ Err: errors.New("not a pointer"),
+ })
}
dst := _dstPtr.Elem()
@@ -52,7 +64,10 @@ func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) {
case reflect.Struct:
return getStructHandler(dst.Type()).Unmarshal(dat, dst)
default:
- panic(fmt.Errorf("type=%v does not implement binfmt.Unmarshaler and kind=%v is not a supported statically-sized kind",
- dst.Type(), dst.Kind()))
+ panic(&InvalidTypeError{
+ Type: _dstPtr.Type(),
+ Err: fmt.Errorf("does not implement binfmt.Unmarshaler and kind=%v is not a supported statically-sized kind",
+ dst.Kind()),
+ })
}
}