reflect: use &zeroVal[0] instead of nil for data field for zero-sized payloads

Because our wrapper functions barf if the pointer is nil, even if
we don't actually dereference the pointer.

Fixes #77779

Change-Id: Ib1b93d9f0fdc771cd884137007508ba2b1da4b7a
Reviewed-on: https://go-review.googlesource.com/c/go/+/748660
Reviewed-by: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Keith Randall
2026-02-24 15:31:08 -08:00
parent fa8595702e
commit 60dc96479a
2 changed files with 48 additions and 2 deletions

View File

@@ -1288,9 +1288,10 @@ func (v Value) Field(i int) Value {
// bunch of zero-sized fields. We must return the zero-sized
// fields indirectly, as only ptr-shaped things can be direct.
// See issue 74935.
// We use nil instead of v.ptr as it doesn't matter and
// We use &zeroVal[0] instead of v.ptr as it doesn't matter and
// we can avoid pinning a possibly now-unused object.
return Value{typ, nil, fl | flagIndir}
// Don't use nil, see issue 77779.
return Value{typ, unsafe.Pointer(&zeroVal[0]), fl | flagIndir}
}
// Either flagIndir is set and v.ptr points at struct,

View File

@@ -0,0 +1,45 @@
// run
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"reflect"
)
type Renderer interface {
Render() error
}
type ZeroSize struct{}
func (ZeroSize) Render() error { return nil }
type Data struct {
X, Y, Z int
}
// Container is pointer-sized (8 bytes): zero-size embed + one pointer field.
// This triggers Go 1.26 interface inlining, which produces a nil data pointer
// for the zero-size field when extracted via reflect.Value.Interface().
type Container struct {
ZeroSize
Data *Data
}
func main() {
render(Container{})
render(&Container{})
}
func render(iface any) {
if reflect.ValueOf(iface).Kind() == reflect.Ptr {
_ = reflect.ValueOf(iface).Elem().Field(0).Interface().(Renderer).Render()
return
}
_ = reflect.ValueOf(iface).Field(0).Interface().(Renderer).Render()
}