Skip to content

Commit 465338b

Browse files
authored
Merge pull request #101 from GoASTScanner/bugfix
Recreate fileset each time we process a file
2 parents b5308ff + 191750f commit 465338b

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

core/analyzer.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ type Metrics struct {
7979
type Analyzer struct {
8080
ignoreNosec bool
8181
ruleset RuleSet
82-
context Context
82+
context *Context
8383
logger *log.Logger
84-
Issues []Issue `json:"issues"`
85-
Stats Metrics `json:"metrics"`
84+
Issues []*Issue `json:"issues"`
85+
Stats *Metrics `json:"metrics"`
8686
}
8787

8888
// NewAnalyzer builds a new anaylzer.
@@ -93,17 +93,10 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer {
9393
a := Analyzer{
9494
ignoreNosec: conf["ignoreNosec"].(bool),
9595
ruleset: make(RuleSet),
96-
Issues: make([]Issue, 0),
97-
context: Context{
98-
token.NewFileSet(),
99-
nil,
100-
nil,
101-
nil,
102-
nil,
103-
nil,
104-
nil,
105-
},
106-
logger: logger,
96+
context: &Context{nil, nil, nil, nil, nil, nil, nil},
97+
logger: logger,
98+
Issues: make([]*Issue, 0, 16),
99+
Stats: &Metrics{0, 0, 0, 0},
107100
}
108101

109102
// TODO(tkelsey): use the inc/exc lists
@@ -113,6 +106,7 @@ func NewAnalyzer(conf map[string]interface{}, logger *log.Logger) Analyzer {
113106

114107
func (gas *Analyzer) process(filename string, source interface{}) error {
115108
mode := parser.ParseComments
109+
gas.context.FileSet = token.NewFileSet()
116110
root, err := parser.ParseFile(gas.context.FileSet, filename, source, mode)
117111
if err == nil {
118112
gas.context.Comments = ast.NewCommentMap(gas.context.FileSet, root, root.Comments)
@@ -221,14 +215,14 @@ func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
221215

222216
if val, ok := gas.ruleset[reflect.TypeOf(n)]; ok {
223217
for _, rule := range val {
224-
ret, err := rule.Match(n, &gas.context)
218+
ret, err := rule.Match(n, gas.context)
225219
if err != nil {
226-
file, line := GetLocation(n, &gas.context)
220+
file, line := GetLocation(n, gas.context)
227221
file = path.Base(file)
228222
gas.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line)
229223
}
230224
if ret != nil {
231-
gas.Issues = append(gas.Issues, *ret)
225+
gas.Issues = append(gas.Issues, ret)
232226
gas.Stats.NumFound++
233227
}
234228
}

core/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestMatchCallByType(t *testing.T) {
5757
t.Errorf("Expected to match a bytes.Buffer.Write call")
5858
}
5959

60-
typeName, callName, err := GetCallInfo(rule.callExpr[0], &analyzer.context)
60+
typeName, callName, err := GetCallInfo(rule.callExpr[0], analyzer.context)
6161
if err != nil {
6262
t.Errorf("Unable to resolve call info: %v\n", err)
6363
}

rules/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
gas "github.com/GoASTScanner/gas/core"
2222
)
2323

24-
func gasTestRunner(source string, analyzer gas.Analyzer) []gas.Issue {
24+
func gasTestRunner(source string, analyzer gas.Analyzer) []*gas.Issue {
2525
analyzer.ProcessSource("dummy.go", source)
2626
return analyzer.Issues
2727
}
2828

29-
func checkTestResults(t *testing.T, issues []gas.Issue, expected int, msg string) {
29+
func checkTestResults(t *testing.T, issues []*gas.Issue, expected int, msg string) {
3030
found := len(issues)
3131
if found != expected {
3232
t.Errorf("Found %d issues, expected %d", found, expected)

0 commit comments

Comments
 (0)