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