Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes based on go-fuzz runs #6

Merged
merged 1 commit into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}