@@ -38,8 +38,7 @@ func (i *integerOverflowCheck) ID() string {
38
38
// TODO: restrict it to just the possible bit-sizes for X (unspecified, 8, 16, 32, 64)
39
39
// TODO: check if y's bit-size is greater than X
40
40
func (i * integerOverflowCheck ) Match (node ast.Node , ctx * gosec.Context ) (* gosec.Issue , error ) {
41
-
42
- // ignore if its protobuf
41
+ // ignore if it's protobuf
43
42
fileName := ctx .FileSet .File (node .Pos ()).Name ()
44
43
if strings .HasSuffix (fileName , ".pb.go" ) {
45
44
return nil , nil
@@ -70,6 +69,19 @@ func (i *integerOverflowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.
70
69
return nil , nil
71
70
}
72
71
72
+ switch arg := arg .(type ) {
73
+ case * ast.CallExpr :
74
+ // len() returns an int that is always >= 0, so it will fit in a uint, uint64, or int64.
75
+ if argFun , ok := arg .Fun .(* ast.Ident ); ok && argFun .Name == "len" && (fun .Name == "uint" || fun .Name == "uint64" || fun .Name == "int64" ) {
76
+ return nil , nil
77
+ }
78
+ case * ast.SelectorExpr :
79
+ // If the argument is being cast to its underlying type, there's no risk.
80
+ if ctx .Info .TypeOf (arg ).Underlying () == ctx .Info .TypeOf (fun ) {
81
+ return nil , nil
82
+ }
83
+ }
84
+
73
85
// TODO: run the go type checker to determine the
74
86
// type of arg so we can check if the type
75
87
// conversion is reducing the bit-size and could overflow.
0 commit comments