Skip to content

Commit

Permalink
reflect: panic on recv channel close
Browse files Browse the repository at this point in the history
It is possible to call `reflect.ValueOf(ch).Close()`
on a recv-only channel. Following the same semantics as send
it should result in a panic.

Fixes golang#61445
  • Loading branch information
mauri870 committed Jul 20, 2023
1 parent 7c1157f commit 4a340c5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,17 @@ func (v Value) capNonSlice() int {
func (v Value) Close() {
v.mustBe(Chan)
v.mustBeExported()
v.close()
}

// internal close
// v is known to be a channel.
func (v Value) close() {
tt := (*chanType)(unsafe.Pointer(v.typ()))
if ChanDir(tt.Dir)&SendDir == 0 {
panic("reflect: close on receive-only channel")
}

chanclose(v.pointer())
}

Expand Down

0 comments on commit 4a340c5

Please sign in to comment.