Skip to content

Commit

Permalink
Fix possible misuse of reflect.StringHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Sep 9, 2021
1 parent 1fc6093 commit 5cb4702
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions bencode/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5cb4702

Please sign in to comment.