Skip to content

Commit

Permalink
fixes based on go-fuzz runs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Cox committed Aug 9, 2015
1 parent 4439385 commit eed84f2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions network/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
// size.
var ErrNotEnoughSpace = errors.New("not enough space")

// ErrUnknownType is returned when attempting to write values of an unknown type
var ErrUnknownType = errors.New("unknown type")

// Buffer contains the binary representation of multiple ValueLists and state
// optimally write the next ValueList.
type Buffer struct {
Expand Down Expand Up @@ -248,7 +251,7 @@ func (b *Buffer) writeValues(values []api.Value) error {
case api.Derive:
binary.Write(b.buffer, binary.BigEndian, uint8(dsTypeDerive))
default:
panic("unexpected type")
return ErrUnknownType
}
}

Expand All @@ -264,7 +267,7 @@ func (b *Buffer) writeValues(values []api.Value) error {
case api.Derive:
binary.Write(b.buffer, binary.BigEndian, int64(v))
default:
panic("unexpected type")
return ErrUnknownType
}
}

Expand Down
13 changes: 13 additions & 0 deletions network/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@ func TestWriteInt(t *testing.T) {
t.Errorf("got %v, want %v", got, want)
}
}

func TestUnknownType(t *testing.T) {
vl, err := Parse([]byte{0x00, 0x06, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, ParseOpts{})
if err != nil {
t.Errorf("Error parsing input %v", err)
}

s1 := NewBuffer(0)
if err := s1.Write(vl[0]); err == nil {
t.Errorf("Writing bad stream should return an error")
}

}
19 changes: 17 additions & 2 deletions network/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,30 @@ func Parse(b []byte, opts ParseOpts) ([]api.ValueList, error) {
return parse(b, None, opts)
}

func readUint16(buf *bytes.Buffer) (uint16, error) {
read := buf.Next(2)
if len(read) != 2 {
return 0, ErrInvalid
}
return binary.BigEndian.Uint16(read), nil
}

func parse(b []byte, sl SecurityLevel, opts ParseOpts) ([]api.ValueList, error) {
var valueLists []api.ValueList

var state api.ValueList
buf := bytes.NewBuffer(b)

for buf.Len() > 0 {
partType := binary.BigEndian.Uint16(buf.Next(2))
partLength := int(binary.BigEndian.Uint16(buf.Next(2)))
partType, err := readUint16(buf)
if err != nil {
return nil, ErrInvalid
}
partLengthUnsigned, err := readUint16(buf)
if err != nil {
return nil, ErrInvalid
}
partLength := int(partLengthUnsigned)

if partLength < 5 || partLength-4 > buf.Len() {
return valueLists, fmt.Errorf("invalid length %d", partLength)
Expand Down
7 changes: 7 additions & 0 deletions network/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ func TestParseString(t *testing.T) {
t.Errorf("got (%q, nil), want (\"\", ErrorInvalid)", got)
}
}

func TestOneByte(t *testing.T) {
_, err := Parse([]byte{0}, ParseOpts{})
if err == nil {
t.Errorf("Parsing byte stream containing single zero byte should return an error")
}
}

0 comments on commit eed84f2

Please sign in to comment.