Skip to content

Commit

Permalink
io: fix MultiReader panic on nil reader.
Browse files Browse the repository at this point in the history
The combination of two prior CLs can cause panics:
* CL/17873: make chained multiReader Read more efficient
* CL/28533: make MultiReader nil exhausted Readers for earlier GC

The first CL allows MultiReader to "inherit" another MultiReader's list of Readers
for efficiency reasons. This is problematic when combined with the
later CL since that can set prior Readers in that list to nil for GC reasons.
This causes panics when two MultiReaders are used together (even synchronously).

To fix this, rather than setting consumed Readers as nil, we set them with
a special eofReader that always returns EOF.

Fixes #18232

Change-Id: I2a9357ab217e3d54d38ea9022d18e4d14f4182d3
Reviewed-on: https://go-review.googlesource.com/34140
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
dsnet committed Dec 8, 2016
1 parent d4b46aa commit 2912544
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/io/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

package io

type eofReader struct{}

func (eofReader) Read([]byte) (int, error) {
return 0, EOF
}

type multiReader struct {
readers []Reader
}

func (mr *multiReader) Read(p []byte) (n int, err error) {
for len(mr.readers) > 0 {
// Optimization to flatten nested multiReaders (Issue 13558)
// Optimization to flatten nested multiReaders (Issue 13558).
if len(mr.readers) == 1 {
if r, ok := mr.readers[0].(*multiReader); ok {
mr.readers = r.readers
Expand All @@ -19,7 +25,9 @@ func (mr *multiReader) Read(p []byte) (n int, err error) {
}
n, err = mr.readers[0].Read(p)
if err == EOF {
mr.readers[0] = nil // permit earlier GC
// Use eofReader instead of nil to avoid nil panic
// after performing flatten (Issue 18232).
mr.readers[0] = eofReader{} // permit earlier GC
mr.readers = mr.readers[1:]
}
if n > 0 || err != EOF {
Expand Down
24 changes: 24 additions & 0 deletions src/io/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,27 @@ func TestMultiReaderFreesExhaustedReaders(t *testing.T) {
t.Fatalf(`ReadFull = %d (%q), %v; want 2, "ar", nil`, n, buf[:n], err)
}
}

func TestInterleavedMultiReader(t *testing.T) {
r1 := strings.NewReader("123")
r2 := strings.NewReader("45678")

mr1 := MultiReader(r1, r2)
mr2 := MultiReader(mr1)

buf := make([]byte, 4)

// Have mr2 use mr1's []Readers.
// Consume r1 (and clear it for GC to handle) and consume part of r2.
n, err := ReadFull(mr2, buf)
if got := string(buf[:n]); got != "1234" || err != nil {
t.Errorf(`ReadFull(mr2) = (%q, %v), want ("1234", nil)`, got, err)
}

// Consume the rest of r2 via mr1.
// This should not panic even though mr2 cleared r1.
n, err = ReadFull(mr1, buf)
if got := string(buf[:n]); got != "5678" || err != nil {
t.Errorf(`ReadFull(mr1) = (%q, %v), want ("5678", nil)`, got, err)
}
}

0 comments on commit 2912544

Please sign in to comment.