@cryptotaxi247 / kubo / commits / d368cb7a4

godep: update go-msgio

Henry committed May 3, 2015 at 04:29 UTC d368cb7a43882fe82a64e53ff60c46be8dd1d77f
6 files changed +77 -2
Godeps/Godeps.json
+1 -1
@@ -169,7 +169,7 @@
169 },
170 {
171 "ImportPath": "github.com/jbenet/go-msgio",
172 - "Rev": "dbae89193876910c736b2ce1291fa8bbcf299d77"
172 + "Rev": "b4f3f1e1c7ec0cbf2fe35d8a45d1c253d224dc72"
173 },
174 {
175 "ImportPath": "github.com/jbenet/go-multiaddr",
Godeps/_workspace/src/github.com/jbenet/go-msgio/.travis.yml new
+9
@@ -0,0 +1,9 @@
1 +language: go
2 +
3 +go:
4 + - 1.3
5 + - 1.4
6 + - release
7 +
8 +script:
9 + - go test -race -cpu=5 -v ./...
Godeps/_workspace/src/github.com/jbenet/go-msgio/fuzz.go new
+23
@@ -0,0 +1,23 @@
1 +// +build gofuzz
2 +
3 +package msgio
4 +
5 +import "bytes"
6 +
7 +// get the go-fuzz tools and build a fuzzer
8 +// $ go get -u github.com/dvyukov/go-fuzz/...
9 +// $ go-fuzz-build github.com/jbenet/go-msgio
10 +
11 +// put a corpus of random (even better if actual, structured) data in a corpus directry
12 +// $ go-fuzz -bin ./msgio-fuzz -corpus corpus -workdir=wdir -timeout=15
13 +
14 +func Fuzz(data []byte) int {
15 + rc := NewReader(bytes.NewReader(data))
16 + // rc := NewVarintReader(bytes.NewReader(data))
17 +
18 + if _, err := rc.ReadMsg(); err != nil {
19 + return 0
20 + }
21 +
22 + return 1
23 +}
Godeps/_workspace/src/github.com/jbenet/go-msgio/fuzz_test.go new
+24
@@ -0,0 +1,24 @@
1 +package msgio
2 +
3 +import (
4 + "strings"
5 + "testing"
6 +)
7 +
8 +func TestReader_CrashOne(t *testing.T) {
9 + rc := NewReader(strings.NewReader("\x83000"))
10 + _, err := rc.ReadMsg()
11 + if err != ErrMsgTooLarge {
12 + t.Error("should get ErrMsgTooLarge")
13 + t.Log(err)
14 + }
15 +}
16 +
17 +func TestVarintReader_CrashOne(t *testing.T) {
18 + rc := NewVarintReader(strings.NewReader("\x9a\xf1\xed\x9a0"))
19 + _, err := rc.ReadMsg()
20 + if err != ErrMsgTooLarge {
21 + t.Error("should get ErrMsgTooLarge")
22 + t.Log(err)
23 + }
24 +}
Godeps/_workspace/src/github.com/jbenet/go-msgio/msgio.go
+14 -1
@@ -2,6 +2,7 @@ package msgio
2
3 import (
4 "encoding/binary"
5 + "errors"
6 "io"
7 "sync"
8
@@ -11,7 +12,13 @@ import (
12 // NBO is NetworkByteOrder
13 var NBO = binary.BigEndian
14
14 -const lengthSize = 4
15 +// ErrMsgTooLarge is returned when the message length is exessive
16 +var ErrMsgTooLarge = errors.New("message too large")
17 +
18 +const (
19 + lengthSize = 4
20 + defaultMaxSize = 8 * 1024 * 1024 // 8mb
21 +)
22
23 // Writer is the msgio Writer interface. It writes len-framed messages.
24 type Writer interface {
@@ -121,6 +128,7 @@ type reader struct {
128 next int
129 pool *mpool.Pool
130 lock sync.Locker
131 + max int // the maximal message size (in bytes) this reader handles
132 }
133
134 // NewReader wraps an io.Reader with a msgio framed reader. The msgio.Reader
@@ -143,6 +151,7 @@ func NewReaderWithPool(r io.Reader, p *mpool.Pool) ReadCloser {
151 next: -1,
152 pool: p,
153 lock: new(sync.Mutex),
154 + max: defaultMaxSize,
155 }
156 }
157
@@ -191,6 +200,10 @@ func (s *reader) ReadMsg() ([]byte, error) {
200 return nil, err
201 }
202
203 + if length > s.max {
204 + return nil, ErrMsgTooLarge
205 + }
206 +
207 msgb := s.pool.Get(uint32(length))
208 if msgb == nil {
209 return nil, io.ErrShortBuffer
Godeps/_workspace/src/github.com/jbenet/go-msgio/varint.go
+6
@@ -67,6 +67,7 @@ type varintReader struct {
67 next int
68 pool *mpool.Pool
69 lock sync.Locker
70 + max int // the maximal message size (in bytes) this reader handles
71 }
72
73 // NewVarintReader wraps an io.Reader with a varint msgio framed reader.
@@ -92,6 +93,7 @@ func NewVarintReaderWithPool(r io.Reader, p *mpool.Pool) ReadCloser {
93 next: -1,
94 pool: p,
95 lock: new(sync.Mutex),
96 + max: defaultMaxSize,
97 }
98 }
99
@@ -141,6 +143,10 @@ func (s *varintReader) ReadMsg() ([]byte, error) {
143 return nil, err
144 }
145
146 + if length > s.max {
147 + return nil, ErrMsgTooLarge
148 + }
149 +
150 msgb := s.pool.Get(uint32(length))
151 if msgb == nil {
152 return nil, io.ErrShortBuffer