Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: fix infinate loop on chunkReader.Close() #71

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions transport/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func (r *chunkReader) readHeader() error {
if _, err := r.r.Discard(2); err != nil {
return err
}
// not stricly needed but it is the responsibility of this function to
// update chunkLeft.
r.chunkLeft = 0
return io.EOF
}

Expand Down Expand Up @@ -213,7 +216,7 @@ func (r *chunkReader) Read(p []byte) (int, error) {
return 0, ErrInvalidIO
}

// still reading existing chunk
// done with existing chunck so grab the next one
if r.chunkLeft <= 0 {
if err := r.readHeader(); err != nil {
return 0, err
Expand All @@ -234,7 +237,7 @@ func (r *chunkReader) ReadByte() (byte, error) {
return 0, ErrInvalidIO
}

// still reading existing chunk
// done with existing chunck so grab the next one
if r.chunkLeft <= 0 {
if err := r.readHeader(); err != nil {
return 0, err
Expand All @@ -255,8 +258,11 @@ func (r *chunkReader) Close() error {
// poison the reader so that it can no longer be used
defer func() { r.r = nil }()

// read all remaining chunks until we get to the end of the frame.
for {
if r.chunkLeft <= 0 {
// readHeader return io.EOF when it encounter the end-of-frame
// marker ("\n##\n")
err := r.readHeader()
switch err {
case nil:
Expand All @@ -268,9 +274,11 @@ func (r *chunkReader) Close() error {
}
}

if _, err := r.r.Discard(r.chunkLeft); err != nil {
n, err := r.r.Discard(r.chunkLeft)
if err != nil {
return err
}
r.chunkLeft -= n
}
}

Expand Down
20 changes: 16 additions & 4 deletions transport/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ var chunkedTests = []struct {
func TestChunkReaderReadByte(t *testing.T) {
for _, tc := range chunkedTests {
t.Run(tc.name, func(t *testing.T) {
r := bufio.NewReader(bytes.NewReader(tc.input))
cr := &chunkReader{r: r}
r := &chunkReader{
r: bufio.NewReader(bytes.NewReader(tc.input)),
}

buf := make([]byte, 8192)

Expand All @@ -87,7 +88,7 @@ func TestChunkReaderReadByte(t *testing.T) {
err error
)
for {
b, err = cr.ReadByte()
b, err = r.ReadByte()
if err != nil {
break
}
Expand All @@ -97,9 +98,12 @@ func TestChunkReaderReadByte(t *testing.T) {
buf = buf[:n]

if err != io.EOF {
assert.Equal(t, err, tc.err)
assert.Equal(t, tc.err, err)
}
assert.Equal(t, tc.want, buf)

// TODO: validate the return error
r.Close()
})
}
}
Expand All @@ -114,6 +118,9 @@ func TestChunkReaderRead(t *testing.T) {
got, err := io.ReadAll(r)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.want, got)

// TODO: validate the return error
r.Close()
})
}
}
Expand Down Expand Up @@ -264,6 +271,9 @@ func TestEOMReadByte(t *testing.T) {
}

assert.Equal(t, tc.want, buf)

// TODO: validate the return error
r.Close()
})
}
}
Expand All @@ -277,6 +287,8 @@ func TestEOMRead(t *testing.T) {
got, err := io.ReadAll(r)
assert.Equal(t, err, tc.err)
assert.Equal(t, tc.want, got)
// TODO: validate the return error
r.Close()
})
}
}
Expand Down
Loading