Merge pull request #1524 from eelcocramer/bufio-fix

Fixes bufio buffer full error when reading from an array larger than 4096 bytes
This commit is contained in:
Vladimir Mihailenco 2020-10-10 12:12:35 +03:00 committed by GitHub
commit b66d05bd63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -69,15 +69,29 @@ func (r *Reader) ReadLine() ([]byte, error) {
// - there is a pending read error; // - there is a pending read error;
// - or line does not end with \r\n. // - or line does not end with \r\n.
func (r *Reader) readLine() ([]byte, error) { func (r *Reader) readLine() ([]byte, error) {
b, err := r.rd.ReadSlice('\n') var s []byte
if err != nil { multi := false
return nil, err for {
b, err := r.rd.ReadSlice('\n')
if err != nil {
// in case the end of the buffer is not reached
if err == bufio.ErrBufferFull {
s = append(s, b...)
multi = true
continue
} else {
return nil, err
}
}
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
}
if multi {
b = append(s, b...)
}
b = b[:len(b)-2]
return b, nil
} }
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
}
b = b[:len(b)-2]
return b, nil
} }
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) { func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {

View File

@ -2,6 +2,7 @@ package proto_test
import ( import (
"bytes" "bytes"
"io"
"testing" "testing"
"github.com/go-redis/redis/v8/internal/proto" "github.com/go-redis/redis/v8/internal/proto"
@ -27,6 +28,21 @@ func BenchmarkReader_ParseReply_Slice(b *testing.B) {
benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", multiBulkParse, false) benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", multiBulkParse, false)
} }
func TestReader_ReadLine(t *testing.T) {
original := bytes.Repeat([]byte("a"), 8192)
original[len(original)-2] = '\r'
original[len(original)-1] = '\n'
r := proto.NewReader(bytes.NewReader(original))
read, err := r.ReadLine()
if err != nil && err != io.EOF {
t.Errorf("Should be able to read the full buffer: %v", err)
}
if bytes.Compare(read, original[:len(original)-2]) != 0 {
t.Errorf("Values must be equal: %d expected %d", len(read), len(original[:len(original)-2]))
}
}
func benchmarkParseReply(b *testing.B, reply string, m proto.MultiBulkParse, wanterr bool) { func benchmarkParseReply(b *testing.B, reply string, m proto.MultiBulkParse, wanterr bool) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {