From 92b364462529c51c2af0f82a3f1fddaa1cf016f4 Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Tue, 30 Apr 2019 08:13:10 +0200 Subject: [PATCH] Fix error parsing when the loaded package is empty Signed-off-by: Cosmin Cojocar --- analyzer.go | 37 +++++++++++++++++++++++-------------- errors.go | 1 - 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/analyzer.go b/analyzer.go index 79d6698..e33bf96 100644 --- a/analyzer.go +++ b/analyzer.go @@ -16,6 +16,7 @@ package gosec import ( + "fmt" "go/ast" "go/build" "go/token" @@ -106,14 +107,16 @@ func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error for _, pkgPath := range packagePaths { pkgs, err := gosec.load(pkgPath, config) if err != nil { - return err + return fmt.Errorf("loading pkg dir %q: %v", pkgPath, err) } for _, pkg := range pkgs { - err := gosec.parseErrors(pkg) - if err != nil { - return err + if pkg.Name != "" { + err := gosec.parseErrors(pkg) + if err != nil { + return fmt.Errorf("parsing errors in pkg %q: %v", pkg.Name, err) + } + gosec.check(pkg) } - gosec.check(pkg) } } sortErrors(gosec.errors) @@ -193,19 +196,25 @@ func (gosec *Analyzer) parseErrors(pkg *packages.Package) error { // at index 3 is the actual error infoErr := strings.Split(pkgErr.Error(), ":") filePath := infoErr[0] - line, err := strconv.Atoi(infoErr[1]) - if err != nil { - return err + var err error + var line, column int + var errorMsg string + if len(infoErr) == 3 { + if line, err = strconv.Atoi(infoErr[1]); err != nil { + return fmt.Errorf("parsing line: %v", err) + } + if column, err = strconv.Atoi(infoErr[2]); err != nil { + return fmt.Errorf("parsing column: %v", err) + } + errorMsg = strings.TrimSpace(infoErr[3]) + } else { + errorMsg = strings.TrimSpace(infoErr[1]) } - column, err := strconv.Atoi(infoErr[2]) - if err != nil { - return err - } - newErr := NewError(line, column, strings.TrimSpace(infoErr[3])) + newErr := NewError(line, column, errorMsg) if errSlice, ok := gosec.errors[filePath]; ok { gosec.errors[filePath] = append(errSlice, *newErr) } else { - errSlice = make([]Error, 0) + errSlice = []Error{} gosec.errors[filePath] = append(errSlice, *newErr) } } diff --git a/errors.go b/errors.go index 3e0edfb..a27aa58 100644 --- a/errors.go +++ b/errors.go @@ -22,7 +22,6 @@ func NewError(line, column int, err string) *Error { // sortErros sorts the golang erros by line func sortErrors(allErrors map[string][]Error) { - for _, errors := range allErrors { sort.Slice(errors, func(i, j int) bool { if errors[i].Line == errors[j].Line {