]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: avoid clearing memory during byte slice allocation in gobytes
authorMartin Möhrmann <moehrmann@google.com>
Sun, 18 Feb 2018 13:12:52 +0000 (14:12 +0100)
committerMartin Möhrmann <moehrmann@google.com>
Mon, 19 Feb 2018 05:58:51 +0000 (05:58 +0000)
Avoid using make in gobytes which clears the byte slice backing
array unnecessarily since the content is overwritten immediately again.

Check that the user provided length is positive and below the maximum
allowed allocation size explicitly in gobytes as this was done in makeslice
before this change.

Fixes #23634

Change-Id: Id852619e932aabfc468871c42ad07d34da91f45c
Reviewed-on: https://go-review.googlesource.com/94760
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/runtime/string.go

index 97909196e9030569284b9c1a89f1174389490dc0..cfe2959b362cfc03717f8f04cc1c45403b3f0973 100644 (file)
@@ -280,13 +280,20 @@ func rawruneslice(size int) (b []rune) {
 }
 
 // used by cmd/cgo
-func gobytes(p *byte, n int) []byte {
+func gobytes(p *byte, n int) (b []byte) {
        if n == 0 {
                return make([]byte, 0)
        }
-       x := make([]byte, n)
-       memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
-       return x
+
+       if n < 0 || uintptr(n) > maxAlloc {
+               panic(errorString("gobytes: length out of range"))
+       }
+
+       bp := mallocgc(uintptr(n), nil, false)
+       memmove(bp, unsafe.Pointer(p), uintptr(n))
+
+       *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
+       return
 }
 
 func gostring(p *byte) string {