From bd760739e52f123b66e89553db9d62a99ae7b1b9 Mon Sep 17 00:00:00 2001 From: Iskander Sharipov Date: Fri, 21 Jan 2022 00:04:58 +0300 Subject: [PATCH] ruleguard: make sub-match gogrep state per-runner Fixes #366 --- ruleguard/filters.go | 6 ++---- ruleguard/gorule.go | 3 ++- ruleguard/runner.go | 21 +++++++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/ruleguard/filters.go b/ruleguard/filters.go index 7d93bcb6..607ea27f 100644 --- a/ruleguard/filters.go +++ b/ruleguard/filters.go @@ -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 diff --git a/ruleguard/gorule.go b/ruleguard/gorule.go index 011a82ce..63907ee2 100644 --- a/ruleguard/gorule.go +++ b/ruleguard/gorule.go @@ -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 diff --git a/ruleguard/runner.go b/ruleguard/runner.go index de8f5bc4..82a40d45 100644 --- a/ruleguard/runner.go +++ b/ruleguard/runner.go @@ -33,7 +33,8 @@ type rulesRunner struct { reportData ReportData - gogrepState gogrep.MatcherState + gogrepState gogrep.MatcherState + gogrepSubState gogrep.MatcherState importer *goImporter @@ -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, @@ -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 }