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 91cebdccde
commit 304f9499cf

View file

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