]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: handle zero-sized fields of directly-stored structures correctly
authorKeith Randall <khr@golang.org>
Thu, 7 Aug 2025 22:30:54 +0000 (15:30 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 8 Aug 2025 13:51:37 +0000 (06:51 -0700)
type W struct {
E struct{}
X *byte
}

type W is a "direct" type. That is, it is a pointer-ish type that can
be stored directly as the second word of an interface.

But if we ask reflect for W's first field, that value must *not* be
direct, as zero-sized things cannot be stored directly.

This was a problem introduced in CL 681937. Before that, types like W
were not eligible for directness.

Fixes #74935

Change-Id: Idefb55c23eaa59153009f863bad611593981e5cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/694195
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
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: Dmitri Shuralyov <dmitshur@google.com>
src/reflect/value.go
test/fixedbugs/issue74935.go [new file with mode: 0644]

index 6f65ef81dc3ae3dea0c989572319c7011c1d4a23..e2ca0d89dd6d9ce87bff7840c0981621748db6c8 100644 (file)
@@ -1277,6 +1277,17 @@ func (v Value) Field(i int) Value {
                        fl |= flagStickyRO
                }
        }
+       if fl&flagIndir == 0 && typ.Size() == 0 {
+               // Special case for picking a field out of a direct struct.
+               // A direct struct must have a pointer field and possibly a
+               // 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 can avoid pinning a possibly now-unused object.
+               return Value{typ, nil, fl | flagIndir}
+       }
+
        // Either flagIndir is set and v.ptr points at struct,
        // or flagIndir is not set and v.ptr is the actual struct data.
        // In the former case, we want v.ptr + offset.
diff --git a/test/fixedbugs/issue74935.go b/test/fixedbugs/issue74935.go
new file mode 100644 (file)
index 0000000..1f6f718
--- /dev/null
@@ -0,0 +1,19 @@
+// run
+
+// Copyright 2025 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 W struct {
+       E struct{}
+       X *byte
+}
+
+func main() {
+       w := reflect.ValueOf(W{})
+       _ = w.Field(0).Interface()
+}