1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:34:39 -07:00

io: prevent write to PipeWriter after Close

Return an ErrClosedPipe rather than allowing the write to proceed.

Fixes #5330.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/12541053
This commit is contained in:
Rick Arnold 2013-08-13 11:04:09 -07:00 committed by Brad Fitzpatrick
parent d3f36dbfc7
commit 4be93851c3
2 changed files with 36 additions and 0 deletions

View File

@ -74,6 +74,10 @@ func (p *pipe) write(b []byte) (n int, err error) {
p.l.Lock()
defer p.l.Unlock()
if p.werr != nil {
err = ErrClosedPipe
return
}
p.data = b
p.rwait.Signal()
for {

View File

@ -268,3 +268,35 @@ func TestWriteNil(t *testing.T) {
ReadFull(r, b[0:2])
r.Close()
}
func TestWriteAfterWriterClose(t *testing.T) {
r, w := Pipe()
done := make(chan bool)
var writeErr error
go func() {
_, err := w.Write([]byte("hello"))
if err != nil {
t.Errorf("got error: %q; expected none", err)
}
w.Close()
_, writeErr = w.Write([]byte("world"))
done <- true
}()
buf := make([]byte, 100)
var result string
n, err := ReadFull(r, buf)
if err != nil && err != ErrUnexpectedEOF {
t.Fatalf("got: %q; want: %q", err, ErrUnexpectedEOF)
}
result = string(buf[0:n])
<-done
if result != "hello" {
t.Errorf("got: %q; want: %q", result, "hello")
}
if writeErr != ErrClosedPipe {
t.Errorf("got: %q; want: %q", writeErr, ErrClosedPipe)
}
}