@@ -38,6 +38,8 @@ const (
38
38
upperBounded
39
39
)
40
40
41
+ const maxDepth = 20
42
+
41
43
func newSliceBoundsAnalyzer (id string , description string ) * analysis.Analyzer {
42
44
return & analysis.Analyzer {
43
45
Name : id ,
@@ -75,7 +77,7 @@ func runSliceBounds(pass *analysis.Pass) (interface{}, error) {
75
77
l , h := extractSliceBounds (slice )
76
78
newCap := computeSliceNewCap (l , h , sliceCap )
77
79
violations := []ssa.Instruction {}
78
- trackSliceBounds (newCap , slice , & violations , ifs )
80
+ trackSliceBounds (0 , newCap , slice , & violations , ifs )
79
81
for _ , s := range violations {
80
82
switch s := s .(type ) {
81
83
case * ssa.Slice :
@@ -155,7 +157,11 @@ func runSliceBounds(pass *analysis.Pass) (interface{}, error) {
155
157
return nil , nil
156
158
}
157
159
158
- func trackSliceBounds (sliceCap int , slice ssa.Node , violations * []ssa.Instruction , ifs map [ssa.If ]* ssa.BinOp ) {
160
+ func trackSliceBounds (depth int , sliceCap int , slice ssa.Node , violations * []ssa.Instruction , ifs map [ssa.If ]* ssa.BinOp ) {
161
+ if depth == maxDepth {
162
+ return
163
+ }
164
+ depth ++
159
165
if violations == nil {
160
166
violations = & []ssa.Instruction {}
161
167
}
@@ -164,12 +170,12 @@ func trackSliceBounds(sliceCap int, slice ssa.Node, violations *[]ssa.Instructio
164
170
for _ , refinstr := range * referrers {
165
171
switch refinstr := refinstr .(type ) {
166
172
case * ssa.Slice :
167
- checkAllSlicesBounds (sliceCap , refinstr , violations , ifs )
173
+ checkAllSlicesBounds (depth , sliceCap , refinstr , violations , ifs )
168
174
switch refinstr .X .(type ) {
169
175
case * ssa.Alloc , * ssa.Parameter :
170
176
l , h := extractSliceBounds (refinstr )
171
177
newCap := computeSliceNewCap (l , h , sliceCap )
172
- trackSliceBounds (newCap , refinstr , violations , ifs )
178
+ trackSliceBounds (depth , newCap , refinstr , violations , ifs )
173
179
}
174
180
case * ssa.IndexAddr :
175
181
indexValue , err := extractIntValue (refinstr .Index .String ())
@@ -189,7 +195,7 @@ func trackSliceBounds(sliceCap int, slice ssa.Node, violations *[]ssa.Instructio
189
195
if fn , ok := refinstr .Call .Value .(* ssa.Function ); ok {
190
196
if len (fn .Params ) > parPos && parPos > - 1 {
191
197
param := fn .Params [parPos ]
192
- trackSliceBounds (sliceCap , param , violations , ifs )
198
+ trackSliceBounds (depth , sliceCap , param , violations , ifs )
193
199
}
194
200
}
195
201
}
@@ -198,7 +204,11 @@ func trackSliceBounds(sliceCap int, slice ssa.Node, violations *[]ssa.Instructio
198
204
}
199
205
}
200
206
201
- func checkAllSlicesBounds (sliceCap int , slice * ssa.Slice , violations * []ssa.Instruction , ifs map [ssa.If ]* ssa.BinOp ) {
207
+ func checkAllSlicesBounds (depth int , sliceCap int , slice * ssa.Slice , violations * []ssa.Instruction , ifs map [ssa.If ]* ssa.BinOp ) {
208
+ if depth == maxDepth {
209
+ return
210
+ }
211
+ depth ++
202
212
if violations == nil {
203
213
violations = & []ssa.Instruction {}
204
214
}
@@ -210,7 +220,7 @@ func checkAllSlicesBounds(sliceCap int, slice *ssa.Slice, violations *[]ssa.Inst
210
220
case * ssa.Alloc , * ssa.Parameter , * ssa.Slice :
211
221
l , h := extractSliceBounds (slice )
212
222
newCap := computeSliceNewCap (l , h , sliceCap )
213
- trackSliceBounds (newCap , slice , violations , ifs )
223
+ trackSliceBounds (depth , newCap , slice , violations , ifs )
214
224
}
215
225
216
226
references := slice .Referrers ()
@@ -220,12 +230,12 @@ func checkAllSlicesBounds(sliceCap int, slice *ssa.Slice, violations *[]ssa.Inst
220
230
for _ , ref := range * references {
221
231
switch s := ref .(type ) {
222
232
case * ssa.Slice :
223
- checkAllSlicesBounds (sliceCap , s , violations , ifs )
233
+ checkAllSlicesBounds (depth , sliceCap , s , violations , ifs )
224
234
switch s .X .(type ) {
225
235
case * ssa.Alloc , * ssa.Parameter :
226
236
l , h := extractSliceBounds (s )
227
237
newCap := computeSliceNewCap (l , h , sliceCap )
228
- trackSliceBounds (newCap , s , violations , ifs )
238
+ trackSliceBounds (depth , newCap , s , violations , ifs )
229
239
}
230
240
}
231
241
}
0 commit comments