]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: improve panic when MapIter has no associated map Value
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 21 May 2021 16:35:46 +0000 (09:35 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Sun, 5 Sep 2021 23:10:46 +0000 (23:10 +0000)
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>
src/reflect/value.go

index 1a61cb897ca2e2da8d5ee23c3ec65a9e5b155047..90f31bae0a9a11d90825a1eb76bd5343a36eba4a 100644 (file)
@@ -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 {