mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 19:45:51 +00:00
Merge pull request #101 from GoASTScanner/bugfix
Recreate fileset each time we process a file
This commit is contained in:
commit
465338b05b
3 changed files with 14 additions and 20 deletions
|
@ -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++
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue