mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 19:45:51 +00:00
Fix error parsing when the loaded package is empty
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
parent
48e39323f3
commit
92b3644625
2 changed files with 23 additions and 15 deletions
29
analyzer.go
29
analyzer.go
|
@ -16,6 +16,7 @@
|
||||||
package gosec
|
package gosec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/build"
|
"go/build"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
@ -106,16 +107,18 @@ func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error
|
||||||
for _, pkgPath := range packagePaths {
|
for _, pkgPath := range packagePaths {
|
||||||
pkgs, err := gosec.load(pkgPath, config)
|
pkgs, err := gosec.load(pkgPath, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("loading pkg dir %q: %v", pkgPath, err)
|
||||||
}
|
}
|
||||||
for _, pkg := range pkgs {
|
for _, pkg := range pkgs {
|
||||||
|
if pkg.Name != "" {
|
||||||
err := gosec.parseErrors(pkg)
|
err := gosec.parseErrors(pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("parsing errors in pkg %q: %v", pkg.Name, err)
|
||||||
}
|
}
|
||||||
gosec.check(pkg)
|
gosec.check(pkg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
sortErrors(gosec.errors)
|
sortErrors(gosec.errors)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -193,19 +196,25 @@ func (gosec *Analyzer) parseErrors(pkg *packages.Package) error {
|
||||||
// at index 3 is the actual error
|
// at index 3 is the actual error
|
||||||
infoErr := strings.Split(pkgErr.Error(), ":")
|
infoErr := strings.Split(pkgErr.Error(), ":")
|
||||||
filePath := infoErr[0]
|
filePath := infoErr[0]
|
||||||
line, err := strconv.Atoi(infoErr[1])
|
var err error
|
||||||
if err != nil {
|
var line, column int
|
||||||
return err
|
var errorMsg string
|
||||||
|
if len(infoErr) == 3 {
|
||||||
|
if line, err = strconv.Atoi(infoErr[1]); err != nil {
|
||||||
|
return fmt.Errorf("parsing line: %v", err)
|
||||||
}
|
}
|
||||||
column, err := strconv.Atoi(infoErr[2])
|
if column, err = strconv.Atoi(infoErr[2]); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("parsing column: %v", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
newErr := NewError(line, column, strings.TrimSpace(infoErr[3]))
|
errorMsg = strings.TrimSpace(infoErr[3])
|
||||||
|
} else {
|
||||||
|
errorMsg = strings.TrimSpace(infoErr[1])
|
||||||
|
}
|
||||||
|
newErr := NewError(line, column, errorMsg)
|
||||||
if errSlice, ok := gosec.errors[filePath]; ok {
|
if errSlice, ok := gosec.errors[filePath]; ok {
|
||||||
gosec.errors[filePath] = append(errSlice, *newErr)
|
gosec.errors[filePath] = append(errSlice, *newErr)
|
||||||
} else {
|
} else {
|
||||||
errSlice = make([]Error, 0)
|
errSlice = []Error{}
|
||||||
gosec.errors[filePath] = append(errSlice, *newErr)
|
gosec.errors[filePath] = append(errSlice, *newErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ func NewError(line, column int, err string) *Error {
|
||||||
|
|
||||||
// sortErros sorts the golang erros by line
|
// sortErros sorts the golang erros by line
|
||||||
func sortErrors(allErrors map[string][]Error) {
|
func sortErrors(allErrors map[string][]Error) {
|
||||||
|
|
||||||
for _, errors := range allErrors {
|
for _, errors := range allErrors {
|
||||||
sort.Slice(errors, func(i, j int) bool {
|
sort.Slice(errors, func(i, j int) bool {
|
||||||
if errors[i].Line == errors[j].Line {
|
if errors[i].Line == errors[j].Line {
|
||||||
|
|
Loading…
Reference in a new issue