Source file test/escape_calls.go

     1  // errorcheck -0 -m -l
     2  
     3  // Copyright 2015 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  // Test escape analysis for function parameters.
     8  
     9  // In this test almost everything is BAD except the simplest cases
    10  // where input directly flows to output.
    11  
    12  package foo
    13  
    14  func f(buf []byte) []byte { // ERROR "leaking param: buf to result ~r0 level=0$"
    15  	return buf
    16  }
    17  
    18  func g(*byte) string
    19  
    20  func h(e int) {
    21  	var x [32]byte // ERROR "moved to heap: x$"
    22  	g(&f(x[:])[0])
    23  }
    24  
    25  type Node struct {
    26  	s           string
    27  	left, right *Node
    28  }
    29  
    30  func walk(np **Node) int { // ERROR "leaking param content: np"
    31  	n := *np
    32  	w := len(n.s)
    33  	if n == nil {
    34  		return 0
    35  	}
    36  	wl := walk(&n.left)
    37  	wr := walk(&n.right)
    38  	if wl < wr {
    39  		n.left, n.right = n.right, n.left // ERROR "ignoring self-assignment"
    40  		wl, wr = wr, wl
    41  	}
    42  	*np = n
    43  	return w + wl + wr
    44  }
    45  
    46  // Test for bug where func var f used prototype's escape analysis results.
    47  func prototype(xyz []string) {} // ERROR "xyz does not escape"
    48  func bar() {
    49  	var got [][]string
    50  	f := prototype
    51  	f = func(ss []string) { got = append(got, ss) } // ERROR "leaking param: ss" "func literal does not escape"
    52  	s := "string"
    53  	f([]string{s}) // ERROR "\[\]string{...} escapes to heap"
    54  }
    55  
    56  func strmin(a, b, c string) string { // ERROR "leaking param: a to result ~r0 level=0" "leaking param: b to result ~r0 level=0" "leaking param: c to result ~r0 level=0"
    57  	return min(a, b, c)
    58  }
    59  func strmax(a, b, c string) string { // ERROR "leaking param: a to result ~r0 level=0" "leaking param: b to result ~r0 level=0" "leaking param: c to result ~r0 level=0"
    60  	return max(a, b, c)
    61  }
    62  

View as plain text