Skip to content

Commit

Permalink
Recreate fileset each time we process a file
Browse files Browse the repository at this point in the history
Some files were being counted multiple times here and giving a skewed
result for line numbers processed.

Closes securego#100
  • Loading branch information
gcmurphy committed Dec 2, 2016
1 parent b5308ff commit 191750f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
28 changes: 11 additions & 17 deletions core/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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++
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions rules/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 191750f

Please sign in to comment.