Skip to content

Commit 8d1c6bf

Browse files
committed
core/cp: m prevent infinite recursion in io.Copy
1 parent 8fbb8d3 commit 8d1c6bf

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

intra/core/cp.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,45 @@ func Stream(dst io.Writer, src io.Reader) (int64, error) {
4949
*bptr = b
5050
Recycle(bptr)
5151
}()
52-
return io.CopyBuffer(dst, src, b)
52+
return io.CopyBuffer(
53+
writerNoReadFrom{Writer: dst},
54+
readerNoWriteTo{Reader: src},
55+
b,
56+
)
57+
}
58+
59+
// from: go-review.googlesource.com/c/go/+/472475/20/src/net/net.go
60+
61+
// noReadFrom can be embedded alongside another type to
62+
// hide the ReadFrom method of that other type.
63+
type noReadFrom struct{}
64+
65+
// ReadFrom hides another ReadFrom method.
66+
// It should never be called.
67+
func (noReadFrom) ReadFrom(io.Reader) (int64, error) {
68+
panic("noReadFrom: hidden func; should not be called")
69+
}
70+
71+
// noWriteTo can be embedded alongside another type to
72+
// hide the WriterTo method of that other type.
73+
type noWriteTo struct{}
74+
75+
func (noWriteTo) WriteTo(io.Writer) (int64, error) {
76+
panic("noWriteTo: hidden func; should not be called")
77+
}
78+
79+
// writerNoReadFrom implements all the methods of io.Writer other
80+
// than ReadFrom. This is used to permit ReadFrom to call io.Copy
81+
// without leading to a recursive call to ReadFrom.
82+
type writerNoReadFrom struct {
83+
noReadFrom
84+
io.Writer
85+
}
86+
87+
// readerNoWriteTo implements all the methods of io.Reader other
88+
// than WriteTo. This is used to permit WriteTo to call io.Copy
89+
// without leading to a recursive call to WriteTo.
90+
type readerNoWriteTo struct {
91+
noWriteTo
92+
io.Reader
5393
}

0 commit comments

Comments
 (0)