Skip to content

Commit 4538cb2

Browse files
authored
ruleguard: pass imports table to gogrep pattern compiler (#343)
This avoids any miscompiled patterns as well as allows more precise matching of package symbols in function call contexts.
1 parent 6b5664f commit 4538cb2

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

analyzer/analyzer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var tests = []struct {
4545
{name: "uber"},
4646
{name: "localfunc"},
4747
{name: "goversion", flags: map[string]string{"go": "1.16"}},
48+
{name: "imports"},
4849
}
4950

5051
func TestDirectiveComments(t *testing.T) {

analyzer/testdata/src/imports/f1.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package imports
2+
3+
import (
4+
crand "crypto/rand"
5+
"math/rand"
6+
)
7+
8+
func _() {
9+
_, _ = crand.Read(nil) // want `\Qcrypto/rand`
10+
_, _ = rand.Read(nil) // want `\Qmath/rand`
11+
}
12+
13+
func _() {
14+
_, _ = rand.Read(nil) // want `\Qmath/rand`
15+
_, _ = crand.Read(nil) // want `\Qcrypto/rand`
16+
}
17+
18+
func _() {
19+
var rand distraction
20+
_, _ = rand.Read(nil)
21+
}
22+
23+
type distraction struct{}
24+
25+
func (distraction) Read(p []byte) (int, error) {
26+
return 0, nil
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package gorules
5+
6+
import (
7+
"github.com/quasilyte/go-ruleguard/dsl"
8+
)
9+
10+
func testMathRand(m dsl.Matcher) {
11+
m.Match(`rand.Read($*_)`).Report(`math/rand`)
12+
}
13+
14+
func testCryptoRand(m dsl.Matcher) {
15+
m.Import(`crypto/rand`)
16+
m.Match(`rand.Read($*_)`).Report(`crypto/rand`)
17+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ require (
77
github.com/google/go-cmp v0.5.6
88
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101150716-969a394a9451
99
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71
10-
github.com/quasilyte/gogrep v0.0.0-20211226113550-e12a97c7d96d
10+
github.com/quasilyte/gogrep v0.0.0-20220103110004-ffaa07af02e3
1111
golang.org/x/tools v0.0.0-20201230224404-63754364767c
1212
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71 h1:CN
2323
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50=
2424
github.com/quasilyte/gogrep v0.0.0-20211226113550-e12a97c7d96d h1:HUyLC9v8wzT8PBFdZjGehLLNSPzQMXDsbREsMHxwma8=
2525
github.com/quasilyte/gogrep v0.0.0-20211226113550-e12a97c7d96d/go.mod h1:wSEyW6O61xRV6zb6My3HxrQ5/8ke7NE2OayqCHa3xRM=
26+
github.com/quasilyte/gogrep v0.0.0-20220103102714-b302ec19c4fe h1:4QqQfYkJRjKR94aebELDiHFj/f+5lFKtQTVOt7luT20=
27+
github.com/quasilyte/gogrep v0.0.0-20220103102714-b302ec19c4fe/go.mod h1:wSEyW6O61xRV6zb6My3HxrQ5/8ke7NE2OayqCHa3xRM=
28+
github.com/quasilyte/gogrep v0.0.0-20220103110004-ffaa07af02e3 h1:P4QPNn+TK49zJjXKERt/vyPbv/mCHB/zQ4flDYOMN+M=
29+
github.com/quasilyte/gogrep v0.0.0-20220103110004-ffaa07af02e3/go.mod h1:wSEyW6O61xRV6zb6My3HxrQ5/8ke7NE2OayqCHa3xRM=
2630
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
2731
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
2832
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

ruleguard/ir_loader.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ func (l *irLoader) loadRuleGroup(group *ir.RuleGroup) error {
253253
}
254254

255255
for _, rule := range group.Rules {
256-
if err := l.loadRule(rule); err != nil {
256+
if err := l.loadRule(group, rule); err != nil {
257257
return err
258258
}
259259
}
260260

261261
return nil
262262
}
263263

264-
func (l *irLoader) loadRule(rule ir.Rule) error {
264+
func (l *irLoader) loadRule(group *ir.RuleGroup, rule ir.Rule) error {
265265
proto := goRule{
266266
line: rule.Line,
267267
group: l.group,
@@ -282,7 +282,7 @@ func (l *irLoader) loadRule(rule ir.Rule) error {
282282
}
283283

284284
for _, pat := range rule.SyntaxPatterns {
285-
if err := l.loadSyntaxRule(proto, info, rule, pat.Value, pat.Line); err != nil {
285+
if err := l.loadSyntaxRule(group, proto, info, rule, pat.Value, pat.Line); err != nil {
286286
return err
287287
}
288288
}
@@ -312,15 +312,24 @@ func (l *irLoader) loadCommentRule(resultProto goRule, rule ir.Rule, src string,
312312
return nil
313313
}
314314

315-
func (l *irLoader) loadSyntaxRule(resultProto goRule, filterInfo filterInfo, rule ir.Rule, src string, line int) error {
315+
func (l *irLoader) loadSyntaxRule(group *ir.RuleGroup, resultProto goRule, filterInfo filterInfo, rule ir.Rule, src string, line int) error {
316316
result := resultProto
317317
result.line = line
318318

319+
var imports map[string]string
320+
if len(group.Imports) != 0 {
321+
imports = make(map[string]string)
322+
for _, imported := range group.Imports {
323+
imports[imported.Name] = imported.Path
324+
}
325+
}
326+
319327
gogrepConfig := gogrep.CompileConfig{
320328
Fset: l.gogrepFset,
321329
Src: src,
322330
Strict: false,
323331
WithTypes: true,
332+
Imports: imports,
324333
}
325334
pat, info, err := gogrep.Compile(gogrepConfig)
326335
if err != nil {

0 commit comments

Comments
 (0)