Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: make sub-match gogrep state per-runner #367

Merged
merged 1 commit into from Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions ruleguard/filters.go
Expand Up @@ -160,16 +160,14 @@ func makeAddressableFilter(src, varname string) filterFunc {
}

func makeVarContainsFilter(src, varname string, pat *gogrep.Pattern) filterFunc {
// TODO: use a shared state here as well?
state := gogrep.NewMatcherState()
return func(params *filterParams) matchFilterResult {
state.CapturePreset = params.match.CaptureList()
params.gogrepSubState.CapturePreset = params.match.CaptureList()
matched := false
gogrep.Walk(params.subNode(varname), func(n ast.Node) bool {
if matched {
return false
}
pat.MatchNode(&state, n, func(m gogrep.MatchData) {
pat.MatchNode(params.gogrepSubState, n, func(m gogrep.MatchData) {
matched = true
})
return true
Expand Down
3 changes: 2 additions & 1 deletion ruleguard/gorule.go
Expand Up @@ -58,7 +58,8 @@ type filterParams struct {
imports map[string]struct{}
env *quasigo.EvalEnv

importer *goImporter
importer *goImporter
gogrepSubState *gogrep.MatcherState

match matchData
nodePath *nodePath
Expand Down
21 changes: 13 additions & 8 deletions ruleguard/runner.go
Expand Up @@ -33,7 +33,8 @@ type rulesRunner struct {

reportData ReportData

gogrepState gogrep.MatcherState
gogrepState gogrep.MatcherState
gogrepSubState gogrep.MatcherState

importer *goImporter

Expand Down Expand Up @@ -67,14 +68,17 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
})
gogrepState := gogrep.NewMatcherState()
gogrepState.Types = ctx.Types
gogrepSubState := gogrep.NewMatcherState()
gogrepSubState.Types = ctx.Types
rr := &rulesRunner{
bgContext: context.Background(),
ctx: ctx,
importer: importer,
rules: rules,
gogrepState: gogrepState,
nodePath: newNodePath(),
truncateLen: ctx.TruncateLen,
bgContext: context.Background(),
ctx: ctx,
importer: importer,
rules: rules,
gogrepState: gogrepState,
gogrepSubState: gogrepSubState,
nodePath: newNodePath(),
truncateLen: ctx.TruncateLen,
filterParams: filterParams{
env: state.env.GetEvalEnv(),
importer: importer,
Expand All @@ -86,6 +90,7 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
}
rr.filterParams.nodeText = rr.nodeText
rr.filterParams.nodePath = &rr.nodePath
rr.filterParams.gogrepSubState = &rr.gogrepSubState
return rr
}

Expand Down