From 7619a4528d506277f44e8d4b70366b5b5633b40f Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 21 May 2021 09:35:46 -0700 Subject: [PATCH] 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 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Go Bot Reviewed-by: Keith Randall Reviewed-by: Brad Fitzpatrick --- src/reflect/value.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/reflect/value.go b/src/reflect/value.go index 1a61cb897ca..90f31bae0a9 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -1686,6 +1686,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.hiter.initialized() { mapiterinit(it.m.typ, it.m.pointer(), &it.hiter) } else {