mirror of
https://github.com/golang/go
synced 2024-11-06 10:36:13 -07:00
reflect: panic on recv channel close
It is possible to call reflect.ValueOf(ch).Close() on a recv-only channel,
while close(ch) is a compile-time error. Following the same reflect
semantics as send and recv this should result in a panic.
Fixes #61445
Change-Id: I2a9ee8f45963593a37bd6df4643dd64fb322f9f9
GitHub-Last-Rev: fe2d5e09f5
GitHub-Pull-Request: golang/go#61453
Reviewed-on: https://go-review.googlesource.com/c/go/+/511295
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
b104a0ef32
commit
f2d709b769
@ -1706,6 +1706,12 @@ func TestChan(t *testing.T) {
|
||||
if i, ok := cv.Recv(); i.Int() != 0 || ok {
|
||||
t.Errorf("after close Recv %d, %t", i.Int(), ok)
|
||||
}
|
||||
// Closing a read-only channel
|
||||
shouldPanic("", func() {
|
||||
c := make(<-chan int, 1)
|
||||
cv := ValueOf(c)
|
||||
cv.Close()
|
||||
})
|
||||
}
|
||||
|
||||
// check creation of unbuffered channel
|
||||
|
@ -1187,10 +1187,16 @@ func (v Value) capNonSlice() int {
|
||||
}
|
||||
|
||||
// Close closes the channel v.
|
||||
// It panics if v's Kind is not Chan.
|
||||
// It panics if v's Kind is not Chan or
|
||||
// v is a receive-only channel.
|
||||
func (v Value) Close() {
|
||||
v.mustBe(Chan)
|
||||
v.mustBeExported()
|
||||
tt := (*chanType)(unsafe.Pointer(v.typ()))
|
||||
if ChanDir(tt.Dir)&SendDir == 0 {
|
||||
panic("reflect: close of receive-only channel")
|
||||
}
|
||||
|
||||
chanclose(v.pointer())
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user