Skip to content

Commit

Permalink
attribute: fix go-fuzz crasher (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Dec 23, 2016
1 parent 5847c34 commit 1149baf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ func (a *Attribute) UnmarshalBinary(b []byte) error {
return errInvalidAttribute
}

if a.Length == 0 {
switch {
// No length, no data
case a.Length == 0:
a.Data = make([]byte, 0)
}
if a.Length > 0 {
// Not enough length for any data
case a.Length < 4:
return errInvalidAttribute
// Data present
case a.Length >= 4:
a.Data = make([]byte, len(b[4:a.Length]))
copy(a.Data, b[4:a.Length])
}
Expand Down
5 changes: 5 additions & 0 deletions attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ func TestUnmarshalAttributes(t *testing.T) {
},
err: errInvalidAttribute,
},
{
name: "fuzz crasher: length 1, too short",
b: []byte("\x01\x0000"),
err: errInvalidAttribute,
},
{
name: "no attributes, length 0",
b: []byte{
Expand Down

0 comments on commit 1149baf

Please sign in to comment.