From 81e8278164c7e74f106ec3813f32fa4f6143dd5b Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Wed, 15 Jan 2020 16:27:56 +0100 Subject: [PATCH] Add the Cgo files to the analysed files and ingonre all non-Go files Signed-off-by: Cosmin Cojocar --- analyzer.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/analyzer.go b/analyzer.go index 52fa750..ca4440c 100644 --- a/analyzer.go +++ b/analyzer.go @@ -24,6 +24,7 @@ import ( "log" "os" "path" + "path/filepath" "reflect" "regexp" "strconv" @@ -174,6 +175,9 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages. for _, filename := range basePackage.GoFiles { packageFiles = append(packageFiles, path.Join(pkgPath, filename)) } + for _, filename := range basePackage.CgoFiles { + packageFiles = append(packageFiles, path.Join(pkgPath, filename)) + } if gosec.tests { testsFiles := []string{} @@ -195,7 +199,13 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages. func (gosec *Analyzer) Check(pkg *packages.Package) { gosec.logger.Println("Checking package:", pkg.Name) for _, file := range pkg.Syntax { - gosec.logger.Println("Checking file:", pkg.Fset.File(file.Pos()).Name()) + checkedFile := pkg.Fset.File(file.Pos()).Name() + // Skip the no-Go file from analysis (e.g. a Cgo files is expanded in 3 different files + // stored in the cache which do not need to by analyzed) + if filepath.Ext(checkedFile) != ".go" { + continue + } + gosec.logger.Println("Checking file:", checkedFile) gosec.context.FileSet = pkg.Fset gosec.context.Config = gosec.config gosec.context.Comments = ast.NewCommentMap(gosec.context.FileSet, file, file.Comments)