modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue19743.go (about)

     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))         // ERROR "toString &s does not escape$"
    26  	strHeader.Data = (*reflect.SliceHeader)(unsafe.Pointer(&b)).Data // ERROR "toString &b does not escape$"
    27  
    28  	l := len(b)
    29  	strHeader.Len = l
    30  	return s
    31  }