| 1 |
// errorcheck -0 -m -l |
| 2 |
|
| 3 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package foo |
| 8 |
|
| 9 |
// Escape analysis needs to treat the uintptr-typed reflect.*Header fields as pointers. |
| 10 |
|
| 11 |
import ( |
| 12 |
"reflect" |
| 13 |
"unsafe" |
| 14 |
) |
| 15 |
|
| 16 |
type immutableBytes []byte |
| 17 |
|
| 18 |
// Bug was failure to leak param b. |
| 19 |
func toString(b immutableBytes) string { // ERROR "leaking param: b$" |
| 20 |
var s string |
| 21 |
if len(b) == 0 { |
| 22 |
return s |
| 23 |
} |
| 24 |
|
| 25 |
strHeader := (*reflect.StringHeader)(unsafe.Pointer(&s)) |
| 26 |
strHeader.Data = (*reflect.SliceHeader)(unsafe.Pointer(&b)).Data |
| 27 |
|
| 28 |
l := len(b) |
| 29 |
strHeader.Len = l |
| 30 |
return s |
| 31 |
} |