mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: make Value.Interface return immutable data
Fixes #3134. R=golang-dev, r CC=golang-dev https://golang.org/cl/5713049
This commit is contained in:
parent
dc159fabff
commit
a72b87efa9
2 changed files with 22 additions and 0 deletions
|
|
@ -842,6 +842,16 @@ func valueInterface(v Value, safe bool) interface{} {
|
|||
var eface emptyInterface
|
||||
eface.typ = v.typ.runtimeType()
|
||||
eface.word = v.iword()
|
||||
|
||||
if v.flag&flagIndir != 0 && v.typ.size > ptrSize {
|
||||
// eface.word is a pointer to the actual data,
|
||||
// which might be changed. We need to return
|
||||
// a pointer to unchanging data, so make a copy.
|
||||
ptr := unsafe_New(v.typ)
|
||||
memmove(ptr, unsafe.Pointer(eface.word), v.typ.size)
|
||||
eface.word = iword(ptr)
|
||||
}
|
||||
|
||||
return *(*interface{})(unsafe.Pointer(&eface))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue