mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
Merge pull request #67 from GoASTScanner/use_types
Add MatchCall helper that utilizes type checker
This commit is contained in:
commit
9fa0b726a0
1 changed files with 27 additions and 0 deletions
|
@ -18,6 +18,7 @@ import (
|
|||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -46,6 +47,32 @@ func MatchCall(n ast.Node, r *regexp.Regexp) *ast.CallExpr {
|
|||
return nil
|
||||
}
|
||||
|
||||
// MatchCallByObject ses the type checker to resolve the associated object with a
|
||||
// particular *ast.CallExpr. This object is used to determine if the
|
||||
// package and identifier name matches the passed in parameters.
|
||||
//
|
||||
// Usage:
|
||||
// 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
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// MatchCompLit will match an ast.CompositeLit if its string value obays the given regex.
|
||||
func MatchCompLit(n ast.Node, r *regexp.Regexp) *ast.CompositeLit {
|
||||
t := reflect.TypeOf(&ast.CompositeLit{})
|
||||
|
|
Loading…
Reference in a new issue