reflect: fix nil array pointer caused panic

Change-Id: I499d3177f219e813e2d88441d04c044feb3081de
GitHub-Last-Rev: 81c4a170d7
GitHub-Pull-Request: golang/go#78928
Reviewed-on: https://go-review.googlesource.com/c/go/+/770302
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Sean Liao <sean@liao.dev>
This commit is contained in:
Weixie Cui 2026-04-25 04:03:40 +00:00 committed by Gopher Robot
parent 5756e857c8
commit d9a6e74180
2 changed files with 16 additions and 3 deletions

View file

@ -68,12 +68,12 @@ func (v Value) Seq() iter.Seq[Value] {
case Uintptr:
return rangeNum[uintptr](v.Uint(), v.Type())
case Pointer:
if v.Elem().kind() != Array {
if v.Type().Elem().Kind() != Array {
break
}
n := v.Type().Elem().Len()
return func(yield func(Value) bool) {
v = v.Elem()
for i := range v.Len() {
for i := range n {
if !yield(ValueOf(i)) {
return
}

View file

@ -91,6 +91,19 @@ func TestValueSeq(t *testing.T) {
t.Fatalf("should loop four times")
}
}},
// Regression: Value.Seq on a nil *[n]int must not panic (Elem of nil ptr is invalid).
{"nil *[3]int", ValueOf((*[3]int)(nil)), func(t *testing.T, s iter.Seq[Value]) {
i := int64(0)
for v := range s {
if v.Int() != i {
t.Fatalf("got %d, want %d", v.Int(), i)
}
i++
}
if i != 3 {
t.Fatalf("should loop three times, got %d", i)
}
}},
{"[4]int", ValueOf([4]int{1, 2, 3, 4}), func(t *testing.T, s iter.Seq[Value]) {
i := int64(0)
for v := range s {