mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 19:45:51 +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")
|
||||
//
|
||||
func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
|
||||
var obj types.Object
|
||||
switch node := n.(type) {
|
||||
case *ast.CallExpr:
|
||||
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
|
||||
}
|
||||
call, obj := GetCallObject(n, c)
|
||||
if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
|
||||
return call, obj
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -113,3 +102,19 @@ func GetString(n ast.Node) (string, error) {
|
|||
}
|
||||
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