Skip to content

Commit 808aaea

Browse files
committed
reflect: inherit all bits of flagRO in Value.Addr()
Currently, Value.Addr() collapses flagRO, which is a combination of flagEmbedRO and flagStickyRO, to flagStickyRO. This causes exported fields of unexported anonymous field from Value.Addr().Elem() read only. This commit fix this by inheriting all bits of flagRO from origin value in Value.Addr(). This should be safe due to following reasons: * Result of Value.Addr() is not CanSet() because of it is not CanAddr() but not flagRO. * Addr().Elem() get same flagRO as origin, so it should behave same as origin in CanSet(). Fixes #32772.
1 parent 48e8569 commit 808aaea

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/reflect/value.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ func (v Value) Addr() Value {
257257
if v.flag&flagAddr == 0 {
258258
panic("reflect.Value.Addr of unaddressable value")
259259
}
260-
return Value{v.typ.ptrTo(), v.ptr, v.flag.ro() | flag(Ptr)}
260+
// Inherits possible read only flags from this value,
261+
// so that later Elem() will restore equivalent value back.
262+
fl := v.flag & flagRO
263+
return Value{v.typ.ptrTo(), v.ptr, fl | flag(Ptr)}
261264
}
262265

263266
// Bool returns v's underlying value.

0 commit comments

Comments
 (0)