Skip to content

Commit 7619a45

Browse files
committed
reflect: improve panic when MapIter has no associated map Value
it := new(reflect.MapIter) it.Next() This generates a nil pointer dereference panic from reflect.Value.pointer. Generate a clearer panic. For #46293 Change-Id: I32a22c797e1ba3a7b4e70b38ceb4dedb44d264fa Reviewed-on: https://go-review.googlesource.com/c/go/+/321890 Trust: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 1b2d794 commit 7619a45

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/reflect/value.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,9 @@ func (it *MapIter) SetValue(dst Value) {
16861686
// entry. It returns false when the iterator is exhausted; subsequent
16871687
// calls to Key, Value, or Next will panic.
16881688
func (it *MapIter) Next() bool {
1689+
if !it.m.IsValid() {
1690+
panic("MapIter.Next called on an iterator that does not have an associated map Value")
1691+
}
16891692
if !it.hiter.initialized() {
16901693
mapiterinit(it.m.typ, it.m.pointer(), &it.hiter)
16911694
} else {

0 commit comments

Comments
 (0)