Skip to content

Commit

Permalink
Fix possible memory confusion in unsafe slice cast (#21)
Browse files Browse the repository at this point in the history
* fix possible memory confusion in unsafe slice cast

* update unsafe.go to use simpler, still safe cast
  • Loading branch information
jlauinger authored Jun 19, 2020
1 parent f2edffe commit 05015c3
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ func unsafeBytes2String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

func unsafeString2Bytes(s string) []byte {
func unsafeString2Bytes(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}

0 comments on commit 05015c3

Please sign in to comment.