summaryrefslogtreecommitdiff
path: root/lib/binstruct/binint/builtins.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/binint/builtins.go
parent41ef03aabf8d6db4f926480fc5ddfec014e342d3 (diff)
Fuzz btrfsitem, and by consequence improve binstruct errors
Diffstat (limited to 'lib/binstruct/binint/builtins.go')
-rw-r--r--lib/binstruct/binint/builtins.go38
1 files changed, 16 insertions, 22 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))