bencode: Only use unsafe.String for go>=1.20

This commit is contained in:
Matt Joiner 2023-04-03 15:10:38 +10:00
parent 872b11bd57
commit e13fd755ee
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
4 changed files with 19 additions and 7 deletions

View File

@ -10,7 +10,6 @@ import (
"runtime"
"strconv"
"sync"
"unsafe"
)
// The default bencode string length limit. This is a poor attempt to prevent excessive memory
@ -256,7 +255,7 @@ func (d *Decoder) parseString(v reflect.Value) error {
d.buf.Grow(length)
b := d.buf.Bytes()[:length]
read(b)
x, err := strconv.ParseBool(unsafe.String(unsafe.SliceData(b), len(b)))
x, err := strconv.ParseBool(bytesAsString(b))
if err != nil {
x = length != 0
}

View File

@ -2,7 +2,6 @@ package bencode
import (
"reflect"
"unsafe"
)
// Wow Go is retarded.
@ -10,7 +9,3 @@ var (
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
)
func bytesAsString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

9
bencode/string.go Normal file
View File

@ -0,0 +1,9 @@
//go:build !go1.20
package bencode
import "unsafe"
func bytesAsString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

9
bencode/string_go120.go Normal file
View File

@ -0,0 +1,9 @@
//go:build go1.20
package bencode
import "unsafe"
func bytesAsString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}