aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/all_test.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2022-08-23 19:10:45 -0700
committerJoseph Tsai <joetsai@digital-static.net>2022-09-01 02:24:14 +0000
commit91ef076562dfcf783074dbd84ad7c6db60fdd481 (patch)
tree6480c791ec9979ec151887da231dc3bc3698c6a5 /src/reflect/all_test.go
parent64b260dbdefcd2205e74d236a7f33d0e6b8f48cb (diff)
downloadgo-91ef076562dfcf783074dbd84ad7c6db60fdd481.tar.xz
reflect: fix Value.SetIterXXX to check for the read-only bit
v.SetIterXXX(i) is semantically identical to v.Set(i.XXX()). If the latter panics for unexported values, so should the former. This change may breaking some programs, but the change is justified under the "Go 1 and the Future of Go Programs" document because the "library has a bug that violates the specification". In this case, the "reflect" package does not accurately match the behavior of the Go language specification. Also, this API was recently released, so the number of users who could be depending on this behavior is hopefully lower. Fixes #54628 Change-Id: If86ede51f286e38093f6697944c089f616525115 Reviewed-on: https://go-review.googlesource.com/c/go/+/425184 Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/reflect/all_test.go')
-rw-r--r--src/reflect/all_test.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index 69d5378049..6cc1c9abad 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -8001,6 +8001,17 @@ func TestSetIter(t *testing.T) {
if got := *y.Interface().(*int); got != b {
t.Errorf("pointer incorrect: got %d want %d", got, b)
}
+
+ // Make sure we panic assigning from an unexported field.
+ m = ValueOf(struct{ m map[string]int }{data}).Field(0)
+ for iter := m.MapRange(); iter.Next(); {
+ shouldPanic("using value obtained using unexported field", func() {
+ k.SetIterKey(iter)
+ })
+ shouldPanic("using value obtained using unexported field", func() {
+ v.SetIterValue(iter)
+ })
+ }
}
func TestMethodCallValueCodePtr(t *testing.T) {