From 97bbed8a924d2d21a9bba801bd5047fa4767f03d Mon Sep 17 00:00:00 2001 From: Eelco Cramer Date: Fri, 9 Oct 2020 11:35:14 +0200 Subject: [PATCH] Fixes issue where a buffer larger than 4096 bytes cannot be read --- internal/proto/reader.go | 7 +++++-- internal/proto/reader_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/proto/reader.go b/internal/proto/reader.go index d9e6c120..6dda42d3 100644 --- a/internal/proto/reader.go +++ b/internal/proto/reader.go @@ -55,10 +55,13 @@ func (r *Reader) Reset(rd io.Reader) { } func (r *Reader) ReadLine() ([]byte, error) { - line, err := r.readLine() - if err != nil { + line, err := r.rd.ReadBytes('\n') + if err != nil && err != io.EOF { return nil, err } + if len(line) == 0 { + return nil, fmt.Errorf("redis: reply is empty") + } if isNilReply(line) { return nil, Nil } diff --git a/internal/proto/reader_test.go b/internal/proto/reader_test.go index 4add5ea0..a57b38b6 100644 --- a/internal/proto/reader_test.go +++ b/internal/proto/reader_test.go @@ -27,6 +27,19 @@ func BenchmarkReader_ParseReply_Slice(b *testing.B) { 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) + r := proto.NewReader(bytes.NewReader(original)) + read, err := r.ReadLine() + if err != nil { + t.Errorf("Should be able to read the full buffer: %v", err) + } + + if bytes.Compare(read, original) != 0 { + t.Errorf("Values must be equal: %q", read) + } +} + func benchmarkParseReply(b *testing.B, reply string, m proto.MultiBulkParse, wanterr bool) { buf := new(bytes.Buffer) for i := 0; i < b.N; i++ {