Skip to content

Commit dc2b0ca

Browse files
authored
Merge pull request yuin#287 from jlauinger/fix-unsafe-slice-cast
Fix possible memory confusion in unsafe slice cast
2 parents 6ff375d + 6504538 commit dc2b0ca

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

utils.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,11 @@ func strCmp(s1, s2 string) int {
255255
}
256256
}
257257

258-
func unsafeFastStringToReadOnlyBytes(s string) []byte {
258+
func unsafeFastStringToReadOnlyBytes(s string) (bs []byte) {
259259
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
260-
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}
261-
return *(*[]byte)(unsafe.Pointer(&bh))
260+
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
261+
bh.Data = sh.Data
262+
bh.Cap = sh.Len
263+
bh.Len = sh.Len
264+
return
262265
}

0 commit comments

Comments
 (0)