Skip to content

Commit

Permalink
fix unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 16, 2024
1 parent 6e0cd18 commit 28c1695
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions internal/conv/string.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package conv

import (
"reflect"
"unsafe"
)

// UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes
// must not be altered after this function is called as it will cause a segmentation fault.
func UnsafeStrToBytes(s string) []byte {
var buf []byte
sHdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
bufHdr.Data = sHdr.Data
bufHdr.Cap = sHdr.Len
bufHdr.Len = sHdr.Len
return buf
return unsafe.Slice(unsafe.StringData(s), len(s)) // ref https://github.com/golang/go/issues/53003#issuecomment-1140276077
}

// UnsafeBytesToStr is meant to make a zero allocation conversion
Expand Down

0 comments on commit 28c1695

Please sign in to comment.