Skip to content

Commit e0db3f4

Browse files
committed
Merge branch 'rand-pkg-helper' of git://github.com/tam7t/gas into tam7t-rand-pkg-helper
2 parents 9f54d25 + afb84ff commit e0db3f4

File tree

1 file changed

+4
-34
lines changed

1 file changed

+4
-34
lines changed

rules/rand.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,20 @@ package rules
1616

1717
import (
1818
"go/ast"
19-
"go/types"
20-
"regexp"
2119

2220
gas "github.com/GoASTScanner/gas/core"
2321
)
2422

2523
type WeakRand struct {
2624
gas.MetaData
27-
pattern *regexp.Regexp
25+
funcName string
2826
packagePath string
2927
}
3028

31-
func matchFuncCall(n ast.Node, c *gas.Context) (types.Object, *ast.Ident) {
32-
call, ok := n.(*ast.CallExpr)
33-
if !ok {
34-
return nil, nil
35-
}
36-
37-
sel, ok := call.Fun.(*ast.SelectorExpr)
38-
if !ok {
39-
return nil, nil
40-
}
41-
42-
id, ok := sel.X.(*ast.Ident)
43-
if !ok {
44-
return nil, nil
45-
}
46-
47-
return c.Info.ObjectOf(id), sel.Sel
48-
}
49-
5029
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
51-
o, f := matchFuncCall(n, c)
52-
53-
if o == nil || f == nil {
54-
return nil, nil
55-
}
56-
57-
pkg, ok := o.(*types.PkgName)
58-
if !ok {
59-
return nil, nil
60-
}
30+
node, _ := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName)
6131

62-
if pkg.Imported().Path() == w.packagePath && w.pattern.MatchString(f.String()) {
32+
if node != nil {
6333
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
6434
}
6535

@@ -68,7 +38,7 @@ func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
6838

6939
func NewWeakRandCheck(conf map[string]interface{}) (r gas.Rule, n ast.Node) {
7040
r = &WeakRand{
71-
pattern: regexp.MustCompile(`^Read$`),
41+
funcName: "Read",
7242
packagePath: "math/rand",
7343
MetaData: gas.MetaData{
7444
Severity: gas.High,

0 commit comments

Comments
 (0)