Skip to content

Commit beef125

Browse files
authored
Exclude maps from slince bounce check rule (#1006)
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 21d13c9 commit beef125

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

rules/slice_bounds.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func (s *sliceOutOfBounds) matchSliceMake(funcCall *ast.CallExpr, sliceName stri
233233
return nil, nil // Unexpected, args should always be 2 or 3
234234
}
235235

236+
// Check if the type of the slice is a map, since they should no be checked.
237+
if _, ok := funcCall.Args[0].(*ast.MapType); ok {
238+
return nil, nil
239+
}
240+
236241
// Check and get the capacity of the slice passed to make. It must be a literal value, since we aren't evaluating the expression.
237242
sliceCapLit, ok := funcCall.Args[capacityArg].(*ast.BasicLit)
238243
if !ok {

testutils/source.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,5 +3966,19 @@ func doStuff(x []int) {
39663966
newSlice2 := x[:6]
39673967
fmt.Println(newSlice2)
39683968
}`}, 2, gosec.NewConfig()},
3969+
{[]string{`
3970+
package main
3971+
3972+
import "fmt"
3973+
3974+
func main() {
3975+
testMap := make(map[string]any, 0)
3976+
testMap["test1"] = map[string]interface{}{
3977+
"test2": map[string]interface{}{
3978+
"value": 0,
3979+
},
3980+
}
3981+
fmt.Println(testMap)
3982+
}`}, 0, gosec.NewConfig()},
39693983
}
39703984
)

0 commit comments

Comments
 (0)