mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Merge pull request #134 from jlauinger/fix-unsafe-slice-cast
Fix possible memory confusion in unsafe slice cast
This commit is contained in:
commit
6eb3819f4b
1 changed files with 6 additions and 3 deletions
|
|
@ -13,8 +13,11 @@ func BytesToReadOnlyString(b []byte) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringToReadOnlyBytes returns bytes converted from given string.
|
// StringToReadOnlyBytes returns bytes converted from given string.
|
||||||
func StringToReadOnlyBytes(s string) []byte {
|
func StringToReadOnlyBytes(s string) (bs []byte) {
|
||||||
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||||
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
||||||
return *(*[]byte)(unsafe.Pointer(&bh))
|
bh.Data = sh.Data
|
||||||
|
bh.Cap = sh.Len
|
||||||
|
bh.Len = sh.Len
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue