aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/reflect/value.go
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2011-10-18 14:06:11 +1100
committerAndrew Gerrand <adg@golang.org>2011-10-18 14:06:11 +1100
commit0584eb2e7779d5bf699702d06acb686cd08bddd2 (patch)
tree291e3db413833a248a5afbb4cb023e23ca78593e /src/pkg/reflect/value.go
parentadfa87c5d754252f2bf428b38560de3d630dbe9d (diff)
downloadgo-0584eb2e7779d5bf699702d06acb686cd08bddd2.tar.gz
go-0584eb2e7779d5bf699702d06acb686cd08bddd2.zip
[release-branch.r58] reflect: disallow Interface method on Value obtained via unexported namerelease.r58.2release-branch.r58
Also remove exp/datafmt that depends on the broken reflect behavior. ««« CL 5267049 / eeca0d4a91a3 reflect: disallow Interface method on Value obtained via unexported name Had been allowing it for use by fmt, but it is too hard to lock down. Fix other packages not to depend on it. R=r, r CC=golang-dev https://golang.org/cl/5266054 »»» R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/5297042
Diffstat (limited to 'src/pkg/reflect/value.go')
-rw-r--r--src/pkg/reflect/value.go31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/pkg/reflect/value.go b/src/pkg/reflect/value.go
index 3abe13e04d..9ece67f5fc 100644
--- a/src/pkg/reflect/value.go
+++ b/src/pkg/reflect/value.go
@@ -841,14 +841,7 @@ func (v Value) CanInterface() bool {
if iv.kind == Invalid {
panic(&ValueError{"reflect.Value.CanInterface", iv.kind})
}
- // TODO(rsc): Check flagRO too. Decide what to do about asking for
- // interface for a value obtained via an unexported field.
- // If the field were of a known type, say chan int or *sync.Mutex,
- // the caller could interfere with the data after getting the
- // interface. But fmt.Print depends on being able to look.
- // Now that reflect is more efficient the special cases in fmt
- // might be less important.
- return v.InternalMethod == 0
+ return v.InternalMethod == 0 && iv.flag&flagRO == 0
}
// Interface returns v's value as an interface{}.
@@ -856,19 +849,25 @@ func (v Value) CanInterface() bool {
// (as opposed to Type.Method), Interface cannot return an
// interface value, so it panics.
func (v Value) Interface() interface{} {
- return v.internal().Interface()
+ return valueInterface(v, true)
}
-func (iv internalValue) Interface() interface{} {
+func valueInterface(v Value, safe bool) interface{} {
+ iv := v.internal()
+ return iv.valueInterface(safe)
+}
+
+func (iv internalValue) valueInterface(safe bool) interface{} {
if iv.method {
panic("reflect.Value.Interface: cannot create interface value for method with bound receiver")
}
- /*
- if v.flag()&noExport != 0 {
- panic("reflect.Value.Interface: cannot return value obtained from unexported struct field")
- }
- */
+ if safe && iv.flag&flagRO != 0 {
+ // Do not allow access to unexported values via Interface,
+ // because they might be pointers that should not be
+ // writable or methods or function that should not be callable.
+ panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
+ }
if iv.kind == Interface {
// Special case: return the element inside the interface.
// Won't recurse further because an interface cannot contain an interface.
@@ -1669,7 +1668,7 @@ func convertForAssignment(what string, addr unsafe.Pointer, dst Type, iv interna
if addr == nil {
addr = unsafe.Pointer(new(interface{}))
}
- x := iv.Interface()
+ x := iv.valueInterface(false)
if dst.NumMethod() == 0 {
*(*interface{})(addr) = x
} else {