lib/bytesutil: use unsafe.String instead of unsafe conversion of slice header to string header

This commit is contained in:
Aliaksandr Valialkin 2024-02-29 17:27:51 +02:00
parent e959f54351
commit 38e0397ebd
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -64,12 +64,12 @@ func roundToNearestPow2(n int) int {
// //
// The returned string is valid only until b is reachable and unmodified. // The returned string is valid only until b is reachable and unmodified.
func ToUnsafeString(b []byte) string { 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. // ToUnsafeBytes converts s to a byte slice without memory allocations.
// //
// The returned byte slice is valid only until s is reachable and unmodified. // 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)) return unsafe.Slice(unsafe.StringData(s), len(s))
} }