Fix possible misuse of reflect.StringHeader

This commit is contained in:
Matt Joiner 2021-09-09 22:49:22 +10:00
parent 1fc6093383
commit 5cb47021ac
1 changed files with 6 additions and 4 deletions

View File

@ -21,8 +21,10 @@ func bytesAsString(b []byte) string {
if len(b) == 0 {
return ""
}
return *(*string)(unsafe.Pointer(&reflect.StringHeader{
uintptr(unsafe.Pointer(&b[0])),
len(b),
}))
// See https://github.com/golang/go/issues/40701.
var s string
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
hdr.Data = uintptr(unsafe.Pointer(&b[0]))
hdr.Len = len(b)
return s
}