Source file
test/escape_slice.go
1
2
3
4
5
6
7
8
9 package escape
10
11 import (
12 "os"
13 "strings"
14 )
15
16 var sink interface{}
17
18 func slice0() {
19 var s []*int
20
21 i := 0
22 s = append(s, &i)
23 _ = s
24 }
25
26 func slice1() *int {
27 var s []*int
28 i := 0
29 s = append(s, &i)
30 return s[0]
31 }
32
33 func slice2() []*int {
34 var s []*int
35 i := 0
36 s = append(s, &i)
37 return s
38 }
39
40 func slice3() *int {
41 var s []*int
42 i := 0
43 s = append(s, &i)
44 for _, p := range s {
45 return p
46 }
47 return nil
48 }
49
50 func slice4(s []*int) {
51 i := 0
52 s[0] = &i
53 }
54
55 func slice5(s []*int) {
56 if s != nil {
57 s = make([]*int, 10)
58 }
59 i := 0
60 s[0] = &i
61 }
62
63 func slice6() {
64 s := make([]*int, 10)
65
66 i := 0
67 s[0] = &i
68 _ = s
69 }
70
71 func slice7() *int {
72 s := make([]*int, 10)
73 i := 0
74 s[0] = &i
75 return s[0]
76 }
77
78 func slice8() {
79 i := 0
80 s := []*int{&i}
81 _ = s
82 }
83
84 func slice9() *int {
85 i := 0
86 s := []*int{&i}
87 return s[0]
88 }
89
90 func slice10() []*int {
91 i := 0
92 s := []*int{&i}
93 return s
94 }
95
96 func slice11() {
97 i := 2
98 s := make([]int, 2, 3)
99 s = make([]int, i, 3)
100 s = make([]int, i, 1)
101 _ = s
102 }
103
104 func slice12(x []int) *[1]int {
105 return (*[1]int)(x)
106 }
107
108 func slice13(x []*int) [1]*int {
109 return [1]*int(x)
110 }
111
112 func envForDir(dir string) []string {
113 env := os.Environ()
114 return mergeEnvLists([]string{"PWD=" + dir}, env)
115 }
116
117 func mergeEnvLists(in, out []string) []string {
118 NextVar:
119 for _, inkv := range in {
120 k := strings.SplitAfterN(inkv, "=", 2)[0]
121 for i, outkv := range out {
122 if strings.HasPrefix(outkv, k) {
123 out[i] = inkv
124 continue NextVar
125 }
126 }
127 out = append(out, inkv)
128 }
129 return out
130 }
131
132 const (
133 IPv4len = 4
134 IPv6len = 16
135 )
136
137 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
138
139 func IPv4(a, b, c, d byte) IP {
140 p := make(IP, IPv6len)
141 copy(p, v4InV6Prefix)
142 p[12] = a
143 p[13] = b
144 p[14] = c
145 p[15] = d
146 return p
147 }
148
149 type IP []byte
150
151 type IPAddr struct {
152 IP IP
153 Zone string
154 }
155
156 type resolveIPAddrTest struct {
157 network string
158 litAddrOrName string
159 addr *IPAddr
160 err error
161 }
162
163 var resolveIPAddrTests = []resolveIPAddrTest{
164 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
165 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
166 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
167 }
168
169 func setupTestData() {
170 resolveIPAddrTests = append(resolveIPAddrTests,
171 []resolveIPAddrTest{
172 {"ip",
173 "localhost",
174 &IPAddr{IP: IPv4(127, 0, 0, 1)},
175 nil},
176 {"ip4",
177 "localhost",
178 &IPAddr{IP: IPv4(127, 0, 0, 1)},
179 nil},
180 }...)
181 }
182
View as plain text