summaryrefslogtreecommitdiff
path: root/lib/binstruct/unmarshal.go
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/unmarshal.go
parent41ef03aabf8d6db4f926480fc5ddfec014e342d3 (diff)
Fuzz btrfsitem, and by consequence improve binstruct errors
Diffstat (limited to 'lib/binstruct/unmarshal.go')
-rw-r--r--lib/binstruct/unmarshal.go23
1 files changed, 19 insertions, 4 deletions
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()),
+ })
}
}