Skip to content

Commit eed84f2

Browse files
author
Ryan Cox
committed
fixes based on go-fuzz runs
1 parent 4439385 commit eed84f2

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

network/buffer.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
// size.
1818
var ErrNotEnoughSpace = errors.New("not enough space")
1919

20+
// ErrUnknownType is returned when attempting to write values of an unknown type
21+
var ErrUnknownType = errors.New("unknown type")
22+
2023
// Buffer contains the binary representation of multiple ValueLists and state
2124
// optimally write the next ValueList.
2225
type Buffer struct {
@@ -248,7 +251,7 @@ func (b *Buffer) writeValues(values []api.Value) error {
248251
case api.Derive:
249252
binary.Write(b.buffer, binary.BigEndian, uint8(dsTypeDerive))
250253
default:
251-
panic("unexpected type")
254+
return ErrUnknownType
252255
}
253256
}
254257

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

network/buffer_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,16 @@ func TestWriteInt(t *testing.T) {
143143
t.Errorf("got %v, want %v", got, want)
144144
}
145145
}
146+
147+
func TestUnknownType(t *testing.T) {
148+
vl, err := Parse([]byte{0x00, 0x06, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, ParseOpts{})
149+
if err != nil {
150+
t.Errorf("Error parsing input %v", err)
151+
}
152+
153+
s1 := NewBuffer(0)
154+
if err := s1.Write(vl[0]); err == nil {
155+
t.Errorf("Writing bad stream should return an error")
156+
}
157+
158+
}

network/parse.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,30 @@ func Parse(b []byte, opts ParseOpts) ([]api.ValueList, error) {
3434
return parse(b, None, opts)
3535
}
3636

37+
func readUint16(buf *bytes.Buffer) (uint16, error) {
38+
read := buf.Next(2)
39+
if len(read) != 2 {
40+
return 0, ErrInvalid
41+
}
42+
return binary.BigEndian.Uint16(read), nil
43+
}
44+
3745
func parse(b []byte, sl SecurityLevel, opts ParseOpts) ([]api.ValueList, error) {
3846
var valueLists []api.ValueList
3947

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

4351
for buf.Len() > 0 {
44-
partType := binary.BigEndian.Uint16(buf.Next(2))
45-
partLength := int(binary.BigEndian.Uint16(buf.Next(2)))
52+
partType, err := readUint16(buf)
53+
if err != nil {
54+
return nil, ErrInvalid
55+
}
56+
partLengthUnsigned, err := readUint16(buf)
57+
if err != nil {
58+
return nil, ErrInvalid
59+
}
60+
partLength := int(partLengthUnsigned)
4661

4762
if partLength < 5 || partLength-4 > buf.Len() {
4863
return valueLists, fmt.Errorf("invalid length %d", partLength)

network/parse_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,10 @@ func TestParseString(t *testing.T) {
7777
t.Errorf("got (%q, nil), want (\"\", ErrorInvalid)", got)
7878
}
7979
}
80+
81+
func TestOneByte(t *testing.T) {
82+
_, err := Parse([]byte{0}, ParseOpts{})
83+
if err == nil {
84+
t.Errorf("Parsing byte stream containing single zero byte should return an error")
85+
}
86+
}

0 commit comments

Comments
 (0)