Skip to content

Commit

Permalink
reflect: improve panic when MapIter has no associated map Value
Browse files Browse the repository at this point in the history
it := new(reflect.MapIter)
it.Next()

This generates a nil pointer dereference panic from reflect.Value.pointer.
Generate a clearer panic.

For golang#46293

Change-Id: I32a22c797e1ba3a7b4e70b38ceb4dedb44d264fa
  • Loading branch information
josharian committed May 21, 2021
1 parent e3c2271 commit 742dd1e
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,9 @@ func (it *MapIter) SetValue(dst Value) {
// entry. It returns false when the iterator is exhausted; subsequent
// calls to Key, Value, or Next will panic.
func (it *MapIter) Next() bool {
if !it.m.IsValid() {
panic("MapIter.Next called on an iterator that does not have an associated map Value")
}
if it.it == nil {
it.it = unsafe.Pointer(&it.hiter)
mapiterinit(it.m.typ, it.m.pointer(), it.it)
Expand Down

0 comments on commit 742dd1e

Please sign in to comment.