]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix possible misuse of reflect.StringHeader
authorMatt Joiner <anacrolix@gmail.com>
Thu, 9 Sep 2021 12:49:22 +0000 (22:49 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 9 Sep 2021 12:49:22 +0000 (22:49 +1000)
bencode/misc.go

index 71199590b5c85eb27534abb7e338e95a186619ca..3b95afcecce7dab27ad793f418ea196408204290 100644 (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
 }