lib/bytesutil: prevent from garbage collecting s before returning from ToUnsafeBytes

This commit is contained in:
Aliaksandr Valialkin 2020-06-03 00:23:20 +03:00
parent f2b04f2efe
commit 937338abdf

View File

@ -2,6 +2,7 @@ package bytesutil
import (
"reflect"
"runtime"
"unsafe"
)
@ -29,5 +30,7 @@ func ToUnsafeBytes(s string) []byte {
slh.Data = sh.Data
slh.Len = sh.Len
slh.Cap = sh.Len
return *(*[]byte)(unsafe.Pointer(&slh))
b := *(*[]byte)(unsafe.Pointer(&slh))
runtime.KeepAlive(s)
return b
}