From 191750f44c8eeca6ad477d49905edf1b1f52344d Mon Sep 17 00:00:00 2001 From: Grant Murphy Date: Fri, 2 Dec 2016 15:21:13 -0800 Subject: [PATCH] Recreate fileset each time we process a file Some files were being counted multiple times here and giving a skewed result for line numbers processed. Closes #100 --- core/analyzer.go | 28 +++++++++++----------------- core/helpers_test.go | 2 +- rules/utils_test.go | 4 ++-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/core/analyzer.go b/core/analyzer.go index 83e3631..ea4ec0c 100644 --- a/core/analyzer.go +++ b/core/analyzer.go @@ -79,10 +79,10 @@ type Metrics struct { type Analyzer struct { ignoreNosec bool ruleset RuleSet - context Context + context *Context logger *log.Logger - Issues []Issue `json:"issues"` - Stats Metrics `json:"metrics"` + Issues []*Issue `json:"issues"` + Stats *Metrics `json:"metrics"` } // NewAnalyzer builds a new anaylzer. @@ -93,17 +93,10 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer { a := Analyzer{ ignoreNosec: conf["ignoreNosec"].(bool), ruleset: make(RuleSet), - Issues: make([]Issue, 0), - context: Context{ - token.NewFileSet(), - nil, - nil, - nil, - nil, - nil, - nil, - }, - logger: logger, + context: &Context{nil, nil, nil, nil, nil, nil, nil}, + logger: logger, + Issues: make([]*Issue, 0, 16), + Stats: &Metrics{0, 0, 0, 0}, } // TODO(tkelsey): use the inc/exc lists @@ -113,6 +106,7 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer { func (gas *Analyzer) process(filename string, source interface{}) error { mode := parser.ParseComments + gas.context.FileSet = token.NewFileSet() root, err := parser.ParseFile(gas.context.FileSet, filename, source, mode) if err == nil { gas.context.Comments = ast.NewCommentMap(gas.context.FileSet, root, root.Comments) @@ -221,14 +215,14 @@ func (gas *Analyzer) Visit(n ast.Node) ast.Visitor { if val, ok := gas.ruleset[reflect.TypeOf(n)]; ok { for _, rule := range val { - ret, err := rule.Match(n, &gas.context) + ret, err := rule.Match(n, gas.context) if err != nil { - file, line := GetLocation(n, &gas.context) + file, line := GetLocation(n, gas.context) file = path.Base(file) gas.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line) } if ret != nil { - gas.Issues = append(gas.Issues, *ret) + gas.Issues = append(gas.Issues, ret) gas.Stats.NumFound++ } } diff --git a/core/helpers_test.go b/core/helpers_test.go index 89648e7..1a7bcda 100644 --- a/core/helpers_test.go +++ b/core/helpers_test.go @@ -57,7 +57,7 @@ func TestMatchCallByType(t *testing.T) { t.Errorf("Expected to match a bytes.Buffer.Write call") } - typeName, callName, err := GetCallInfo(rule.callExpr[0], &analyzer.context) + typeName, callName, err := GetCallInfo(rule.callExpr[0], analyzer.context) if err != nil { t.Errorf("Unable to resolve call info: %v\n", err) } diff --git a/rules/utils_test.go b/rules/utils_test.go index 92f3ea3..48fa36f 100644 --- a/rules/utils_test.go +++ b/rules/utils_test.go @@ -21,12 +21,12 @@ import ( gas "github.com/GoASTScanner/gas/core" ) -func gasTestRunner(source string, analyzer gas.Analyzer) []gas.Issue { +func gasTestRunner(source string, analyzer gas.Analyzer) []*gas.Issue { analyzer.ProcessSource("dummy.go", source) return analyzer.Issues } -func checkTestResults(t *testing.T, issues []gas.Issue, expected int, msg string) { +func checkTestResults(t *testing.T, issues []*gas.Issue, expected int, msg string) { found := len(issues) if found != expected { t.Errorf("Found %d issues, expected %d", found, expected)