Skip to content

Commit 066ac42

Browse files
mwhudsonrsc
authored andcommitted
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>
1 parent 78e6abd commit 066ac42

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/reflect/all_test.go

Lines changed: 10 additions & 5 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

@@ -6053,7 +6060,6 @@ func TestUnaddressableField(t *testing.T) {
60536060
})
60546061
}
60556062

6056-
60576063
type Tint int
60586064

60596065
type Tint2 = Tint
@@ -6086,4 +6092,3 @@ func TestAliasNames(t *testing.T) {
60866092
t.Errorf("Talias2 print:\nhave: %s\nwant: %s", out, want)
60876093
}
60886094
}
6089-

src/reflect/type.go

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

14651465
pp.str = resolveReflectName(newName(s, "", "", false))
1466+
pp.ptrToThis = 0
14661467

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

0 commit comments

Comments
 (0)