Skip to content

Commit 7cec9a5

Browse files
mwhudsonrsc
authored andcommitted
[release-branch.go1.8] reflect: clear ptrToThis in Ptr when allocating result on heap
Otherwise, calling PtrTo on the result will fail. Fixes #19003 Change-Id: I8d7d1981a5d0417d5aee52740469d71e90734963 Reviewed-on: https://go-review.googlesource.com/36731 Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-on: https://go-review.googlesource.com/36718 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent d84dee0 commit 7cec9a5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/reflect/all_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,17 +2478,24 @@ func TestNumMethodOnDDD(t *testing.T) {
24782478
}
24792479

24802480
func TestPtrTo(t *testing.T) {
2481+
// This block of code means that the ptrToThis field of the
2482+
// reflect data for *unsafe.Pointer is non zero, see
2483+
// https://golang.org/issue/19003
2484+
var x unsafe.Pointer
2485+
var y = &x
2486+
var z = &y
2487+
24812488
var i int
24822489

2483-
typ := TypeOf(i)
2490+
typ := TypeOf(z)
24842491
for i = 0; i < 100; i++ {
24852492
typ = PtrTo(typ)
24862493
}
24872494
for i = 0; i < 100; i++ {
24882495
typ = typ.Elem()
24892496
}
2490-
if typ != TypeOf(i) {
2491-
t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, TypeOf(i))
2497+
if typ != TypeOf(z) {
2498+
t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, TypeOf(z))
24922499
}
24932500
}
24942501

src/reflect/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,7 @@ func (t *rtype) ptrTo() *rtype {
14691469
pp := *prototype
14701470

14711471
pp.str = resolveReflectName(newName(s, "", "", false))
1472+
pp.ptrToThis = 0
14721473

14731474
// For the type structures linked into the binary, the
14741475
// compiler provides a good hash of the string.

0 commit comments

Comments
 (0)