From 38e0397ebd6463a2d0a8dfd17ad9a61d431703e1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 29 Feb 2024 17:27:51 +0200 Subject: [PATCH] lib/bytesutil: use unsafe.String instead of unsafe conversion of slice header to string header --- lib/bytesutil/bytesutil.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bytesutil/bytesutil.go b/lib/bytesutil/bytesutil.go index 67c67636b..969e63e83 100644 --- a/lib/bytesutil/bytesutil.go +++ b/lib/bytesutil/bytesutil.go @@ -64,12 +64,12 @@ func roundToNearestPow2(n int) int { // // The returned string is valid only until b is reachable and unmodified. func ToUnsafeString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) + return unsafe.String(unsafe.SliceData(b), len(b)) } // ToUnsafeBytes converts s to a byte slice without memory allocations. // // The returned byte slice is valid only until s is reachable and unmodified. -func ToUnsafeBytes(s string) (b []byte) { +func ToUnsafeBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) }