bencode: Only use unsafe.String for go>=1.20
This commit is contained in:
parent
872b11bd57
commit
e13fd755ee
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
//go:build !go1.20
|
||||
|
||||
package bencode
|
||||
|
||||
import "unsafe"
|
||||
|
||||
func bytesAsString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
|
@ -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))
|
||||
}
|
Loading…
Reference in New Issue