From 937338abdf8f3060f66027b9b2e600eab2598963 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 3 Jun 2020 00:23:20 +0300 Subject: [PATCH] lib/bytesutil: prevent from garbage collecting s before returning from ToUnsafeBytes --- lib/bytesutil/bytesutil.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/bytesutil/bytesutil.go b/lib/bytesutil/bytesutil.go index 4ceffab14..cee2e7257 100644 --- a/lib/bytesutil/bytesutil.go +++ b/lib/bytesutil/bytesutil.go @@ -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 }