mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
Split out MatchCallByObject into two functions
Allows direct call to GetCallObject.
This commit is contained in:
parent
9fa0b726a0
commit
0fef3ad40a
1 changed files with 19 additions and 14 deletions
|
@ -55,20 +55,9 @@ func MatchCall(n ast.Node, r *regexp.Regexp) *ast.CallExpr {
|
||||||
// node, obj := MatchCall(n, ctx, "math/rand", "Read")
|
// node, obj := MatchCall(n, ctx, "math/rand", "Read")
|
||||||
//
|
//
|
||||||
func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
|
func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
|
||||||
var obj types.Object
|
call, obj := GetCallObject(n, c)
|
||||||
switch node := n.(type) {
|
if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
|
||||||
case *ast.CallExpr:
|
return call, obj
|
||||||
switch fn := node.Fun.(type) {
|
|
||||||
case *ast.Ident:
|
|
||||||
obj = c.Info.Uses[fn]
|
|
||||||
case *ast.SelectorExpr:
|
|
||||||
obj = c.Info.Uses[fn.Sel]
|
|
||||||
default:
|
|
||||||
obj = nil
|
|
||||||
}
|
|
||||||
if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
|
|
||||||
return node, obj
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -113,3 +102,19 @@ func GetString(n ast.Node) (string, error) {
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("Unexpected AST node type: %T", n)
|
return "", fmt.Errorf("Unexpected AST node type: %T", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCallObject returns the object and call expression and associated
|
||||||
|
// object for a given AST node. nil, nil will be returned if the
|
||||||
|
// object cannot be resolved.
|
||||||
|
func GetCallObject(n ast.Node, ctx *Context) (*ast.CallExpr, types.Object) {
|
||||||
|
switch node := n.(type) {
|
||||||
|
case *ast.CallExpr:
|
||||||
|
switch fn := node.Fun.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
return node, ctx.Info.Uses[fn]
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
return node, ctx.Info.Uses[fn.Sel]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue