Skip to content

Commit

Permalink
Fix s2b (#1079)
Browse files Browse the repository at this point in the history
Ensure `len(b)` <= `cap(b)` at all times. Additionally, use `runtime.KeepAlive()` to prevent `s` from being GC-ed.
  • Loading branch information
YenForYang committed Aug 26, 2021
1 parent a50f59b commit c7ce95f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bytesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"time"
"unsafe"
"runtime"
)

// AppendHTMLEscape appends html-escaped s to dst and returns the extended dst.
Expand Down Expand Up @@ -345,8 +346,9 @@ func s2b(s string) (b []byte) {
/* #nosec G103 */
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
bh.Len = sh.Len
runtime.KeepAlive(&s)
return b
}

Expand Down

0 comments on commit c7ce95f

Please sign in to comment.