From 8c235252b6c626d755144a5e3b029cf65e83caf1 Mon Sep 17 00:00:00 2001 From: todd Date: Thu, 6 Oct 2022 21:31:22 +0800 Subject: [PATCH] Security Optimization string to byte slice --- internal/bytesconv/bytesconv.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/internal/bytesconv/bytesconv.go b/internal/bytesconv/bytesconv.go index 86e4c4d4..d40d7d36 100644 --- a/internal/bytesconv/bytesconv.go +++ b/internal/bytesconv/bytesconv.go @@ -4,21 +4,12 @@ package bytesconv -import ( - "unsafe" -) - -// StringToBytes converts string to byte slice without a memory allocation. +// StringToBytes converts string to byte slice func StringToBytes(s string) []byte { - return *(*[]byte)(unsafe.Pointer( - &struct { - string - Cap int - }{s, len(s)}, - )) + return []byte(s) } -// BytesToString converts byte slice to string without a memory allocation. +// BytesToString converts byte slice to string func BytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) + return string(b) }