@@ -30,40 +30,37 @@ func (i *integerOverflowCheck) ID() string {
30
30
}
31
31
32
32
func (i * integerOverflowCheck ) Match (node ast.Node , ctx * gosec.Context ) (* gosec.Issue , error ) {
33
- var atoiVarName map [string ]ast.Node
33
+ var atoiVarObj map [* ast. Object ]ast.Node
34
34
35
35
// To check multiple lines, ctx.PassedValues is used to store temporary data.
36
36
if _ , ok := ctx .PassedValues [i .ID ()]; ! ok {
37
- atoiVarName = make (map [string ]ast.Node )
38
- ctx .PassedValues [i .ID ()] = atoiVarName
39
- } else if pv , ok := ctx .PassedValues [i .ID ()].(map [string ]ast.Node ); ok {
40
- atoiVarName = pv
37
+ atoiVarObj = make (map [* ast. Object ]ast.Node )
38
+ ctx .PassedValues [i .ID ()] = atoiVarObj
39
+ } else if pv , ok := ctx .PassedValues [i .ID ()].(map [* ast. Object ]ast.Node ); ok {
40
+ atoiVarObj = pv
41
41
} else {
42
- return nil , fmt .Errorf ("PassedValues[%s] of Context is not map[string ]ast.Node, but %T" , i .ID (), ctx .PassedValues [i .ID ()])
42
+ return nil , fmt .Errorf ("PassedValues[%s] of Context is not map[*ast.Object ]ast.Node, but %T" , i .ID (), ctx .PassedValues [i .ID ()])
43
43
}
44
44
45
45
// strconv.Atoi is a common function.
46
46
// To reduce false positives, This rule detects code which is converted to int32/int16 only.
47
47
switch n := node .(type ) {
48
- case * ast.FuncDecl :
49
- // Clear atoiVarName by function
50
- ctx .PassedValues [i .ID ()] = make (map [string ]ast.Node )
51
48
case * ast.AssignStmt :
52
49
for _ , expr := range n .Rhs {
53
50
if callExpr , ok := expr .(* ast.CallExpr ); ok && i .calls .ContainsCallExpr (callExpr , ctx , false ) != nil {
54
51
if idt , ok := n .Lhs [0 ].(* ast.Ident ); ok && idt .Name != "_" {
55
52
// Example:
56
53
// v, _ := strconv.Atoi("1111")
57
- // Add "v" to atoiVarName map
58
- atoiVarName [idt .Name ] = n
54
+ // Add v's Obj to atoiVarObj map
55
+ atoiVarObj [idt .Obj ] = n
59
56
}
60
57
}
61
58
}
62
59
case * ast.CallExpr :
63
60
if fun , ok := n .Fun .(* ast.Ident ); ok {
64
61
if fun .Name == "int32" || fun .Name == "int16" {
65
62
if idt , ok := n .Args [0 ].(* ast.Ident ); ok {
66
- if n , ok := atoiVarName [idt .Name ]; ok {
63
+ if n , ok := atoiVarObj [idt .Obj ]; ok {
67
64
// Detect int32(v) and int16(v)
68
65
return gosec .NewIssue (ctx , n , i .ID (), i .What , i .Severity , i .Confidence ), nil
69
66
}
0 commit comments