Skip to content

Commit a8a7f18

Browse files
author
Chris Manghane
committed
cmd/gc: make embedded, unexported fields read-only.
Fixes #7363. LGTM=gri R=gri, rsc, bradfitz CC=golang-codereviews https://golang.org/cl/66510044
1 parent 15ec569 commit a8a7f18

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/cmd/gc/reflect.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,8 @@ dtypesym(Type *t)
11271127
ot = dgopkgpath(s, ot, t1->sym->pkg);
11281128
} else {
11291129
ot = dgostringptr(s, ot, nil);
1130-
if(t1->type->sym != S && t1->type->sym->pkg == builtinpkg)
1130+
if(t1->type->sym != S &&
1131+
(t1->type->sym->pkg == builtinpkg || !exportname(t1->type->sym->name)))
11311132
ot = dgopkgpath(s, ot, localpkg);
11321133
else
11331134
ot = dgostringptr(s, ot, nil);

test/fixedbugs/issue7363.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run
2+
3+
// Copyright 2014 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// issue 7363: CanSet must return false for unexported embedded struct fields.
8+
9+
package main
10+
11+
import "reflect"
12+
13+
type a struct {
14+
}
15+
16+
type B struct {
17+
a
18+
}
19+
20+
func main() {
21+
b := &B{}
22+
v := reflect.ValueOf(b).Elem().Field(0)
23+
if v.CanSet() {
24+
panic("B.a is an unexported embedded struct field")
25+
}
26+
}

0 commit comments

Comments
 (0)