Skip to content

Commit cfeebb8

Browse files
authored
rules/sdk: handle two integer overflow edge cases (cosmos#20)
1 parent 7ff0887 commit cfeebb8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

rules/sdk/integer.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ func (i *integerOverflowCheck) ID() string {
3838
// TODO: restrict it to just the possible bit-sizes for X (unspecified, 8, 16, 32, 64)
3939
// TODO: check if y's bit-size is greater than X
4040
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
4342
fileName := ctx.FileSet.File(node.Pos()).Name()
4443
if strings.HasSuffix(fileName, ".pb.go") {
4544
return nil, nil
@@ -70,6 +69,19 @@ func (i *integerOverflowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.
7069
return nil, nil
7170
}
7271

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+
7385
// TODO: run the go type checker to determine the
7486
// type of arg so we can check if the type
7587
// conversion is reducing the bit-size and could overflow.

0 commit comments

Comments
 (0)