Skip to content

Commit ac87d84

Browse files
adonovangopherbot
authored andcommitted
internal/event/label: unsafe unsafe.String{,Data} not StringHeader
The old way is deprecated and appears to create a liveness hole (i.e. hides a pointer from the GC), though the compiler does seem to have a special case for it (see ir.IsReflectHeaderDataField). Change-Id: I3e2d3b0d7a8d7b58d0adca55f5c8e87fce9c4d90 Reviewed-on: https://go-review.googlesource.com/c/tools/+/725900 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent c3feb70 commit ac87d84

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

internal/event/label/label.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package label
77
import (
88
"fmt"
99
"io"
10-
"reflect"
1110
"slices"
1211
"unsafe"
1312
)
@@ -103,11 +102,10 @@ type stringptr unsafe.Pointer
103102
// This method is for implementing new key types, label creation should
104103
// normally be done with the Of method of the key.
105104
func OfString(k Key, v string) Label {
106-
hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
107105
return Label{
108106
key: k,
109-
packed: uint64(hdr.Len),
110-
untyped: stringptr(hdr.Data),
107+
packed: uint64(len(v)),
108+
untyped: stringptr(unsafe.StringData(v)),
111109
}
112110
}
113111

@@ -116,11 +114,7 @@ func OfString(k Key, v string) Label {
116114
// This method is for implementing new key types, for type safety normal
117115
// access should be done with the From method of the key.
118116
func (t Label) UnpackString() string {
119-
var v string
120-
hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
121-
hdr.Data = uintptr(t.untyped.(stringptr))
122-
hdr.Len = int(t.packed)
123-
return v
117+
return unsafe.String((*byte)(t.untyped.(stringptr)), int(t.packed))
124118
}
125119

126120
// Valid returns true if the Label is a valid one (it has a key).

0 commit comments

Comments
 (0)