Skip to content

Commit 20831c4

Browse files
authored
ruleguard: make sub-match gogrep state per-runner (#367)
Fixes #366
1 parent c57998e commit 20831c4

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

ruleguard/filters.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,14 @@ func makeAddressableFilter(src, varname string) filterFunc {
160160
}
161161

162162
func makeVarContainsFilter(src, varname string, pat *gogrep.Pattern) filterFunc {
163-
// TODO: use a shared state here as well?
164-
state := gogrep.NewMatcherState()
165163
return func(params *filterParams) matchFilterResult {
166-
state.CapturePreset = params.match.CaptureList()
164+
params.gogrepSubState.CapturePreset = params.match.CaptureList()
167165
matched := false
168166
gogrep.Walk(params.subNode(varname), func(n ast.Node) bool {
169167
if matched {
170168
return false
171169
}
172-
pat.MatchNode(&state, n, func(m gogrep.MatchData) {
170+
pat.MatchNode(params.gogrepSubState, n, func(m gogrep.MatchData) {
173171
matched = true
174172
})
175173
return true

ruleguard/gorule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ type filterParams struct {
5858
imports map[string]struct{}
5959
env *quasigo.EvalEnv
6060

61-
importer *goImporter
61+
importer *goImporter
62+
gogrepSubState *gogrep.MatcherState
6263

6364
match matchData
6465
nodePath *nodePath

ruleguard/runner.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type rulesRunner struct {
3333

3434
reportData ReportData
3535

36-
gogrepState gogrep.MatcherState
36+
gogrepState gogrep.MatcherState
37+
gogrepSubState gogrep.MatcherState
3738

3839
importer *goImporter
3940

@@ -67,14 +68,17 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
6768
})
6869
gogrepState := gogrep.NewMatcherState()
6970
gogrepState.Types = ctx.Types
71+
gogrepSubState := gogrep.NewMatcherState()
72+
gogrepSubState.Types = ctx.Types
7073
rr := &rulesRunner{
71-
bgContext: context.Background(),
72-
ctx: ctx,
73-
importer: importer,
74-
rules: rules,
75-
gogrepState: gogrepState,
76-
nodePath: newNodePath(),
77-
truncateLen: ctx.TruncateLen,
74+
bgContext: context.Background(),
75+
ctx: ctx,
76+
importer: importer,
77+
rules: rules,
78+
gogrepState: gogrepState,
79+
gogrepSubState: gogrepSubState,
80+
nodePath: newNodePath(),
81+
truncateLen: ctx.TruncateLen,
7882
filterParams: filterParams{
7983
env: state.env.GetEvalEnv(),
8084
importer: importer,
@@ -86,6 +90,7 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
8690
}
8791
rr.filterParams.nodeText = rr.nodeText
8892
rr.filterParams.nodePath = &rr.nodePath
93+
rr.filterParams.gogrepSubState = &rr.gogrepSubState
8994
return rr
9095
}
9196

0 commit comments

Comments
 (0)