github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/analysis/passes/unsafeptr/testdata/src/a/issue40701.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package a
     6  
     7  import (
     8  	"reflect"
     9  	"unsafe"
    10  )
    11  
    12  // Explicitly allocating a variable of type reflect.SliceHeader.
    13  func _(p *byte, n int) []byte {
    14  	var sh reflect.SliceHeader
    15  	sh.Data = uintptr(unsafe.Pointer(p))
    16  	sh.Len = n
    17  	sh.Cap = n
    18  	return *(*[]byte)(unsafe.Pointer(&sh)) // want "possible misuse of reflect.SliceHeader"
    19  }
    20  
    21  // Implicitly allocating a variable of type reflect.SliceHeader.
    22  func _(p *byte, n int) []byte {
    23  	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ // want "possible misuse of reflect.SliceHeader"
    24  		Data: uintptr(unsafe.Pointer(p)),
    25  		Len:  n,
    26  		Cap:  n,
    27  	}))
    28  }
    29  
    30  // Use reflect.StringHeader as a composite literal value.
    31  func _(p *byte, n int) []byte {
    32  	var res []byte
    33  	*(*reflect.StringHeader)(unsafe.Pointer(&res)) = reflect.StringHeader{ // want "possible misuse of reflect.StringHeader"
    34  		Data: uintptr(unsafe.Pointer(p)),
    35  		Len:  n,
    36  	}
    37  	return res
    38  }
    39  
    40  func _() {
    41  	// don't crash when obj.Pkg() == nil
    42  	var err error
    43  	_ = &err
    44  }