Skip to content

Commit 1f68d82

Browse files
committed
fix: Guard buffer peek size
1 parent bd7a8b3 commit 1f68d82

File tree

7 files changed

+12
-246
lines changed

7 files changed

+12
-246
lines changed

common/buf/buffer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ func (b *Buffer) Read(data []byte) (n int, err error) {
205205
}
206206

207207
func (b *Buffer) Peek(n int) (bytes []byte, err error) {
208+
if n < 0 {
209+
return nil, ErrNegativeRead
210+
}
208211
if b.start+n > b.end {
209212
return nil, io.ErrShortBuffer
210213
}

common/buf/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package buf
2+
3+
import "errors"
4+
5+
var ErrNegativeRead = errors.New("read with negative size")

common/buf/readv_posix.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

common/buf/readv_reader.go

Lines changed: 0 additions & 122 deletions
This file was deleted.

common/buf/readv_unix.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

common/buf/readv_windows.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

common/bufio/cached.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bufio
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"net"
78

@@ -57,6 +58,9 @@ func (c *CachedConn) Read(p []byte) (n int, err error) {
5758
}
5859

5960
func (c *CachedConn) Peek(n int) ([]byte, error) {
61+
if n < 0 {
62+
return nil, fmt.Errorf("peek %d bytes: %w", n, buf.ErrNegativeRead)
63+
}
6064
if c.cache == nil {
6165
// see above
6266
c.cache = buf.NewSize(4096)

0 commit comments

Comments
 (0)