Recover from panics from the stdlib when cataloging malformed binaries (#663)

* recover from panics in stdlib binary parsing

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add CLI test to cover regression case

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2021-12-10 11:49:50 -05:00
committed by GitHub
parent da0b17b719
commit ab9fe53ff2
7 changed files with 79 additions and 24 deletions
+16 -5
View File
@@ -2,6 +2,7 @@ package golang
import (
"bufio"
"fmt"
"io"
"strings"
@@ -14,19 +15,29 @@ const (
replaceIdentifier = "=>"
)
func parseGoBin(location source.Location, reader io.ReadCloser) ([]pkg.Package, error) {
type exeOpener func(file io.ReadCloser) ([]exe, error)
func parseGoBin(location source.Location, reader io.ReadCloser, opener exeOpener) (pkgs []pkg.Package, err error) {
var exes []exe
// it has been found that there are stdlib paths within openExe that can panic. We want to prevent this behavior
// bubbling up and halting execution. For this reason we try to recover from any panic and return an error.
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic while parse go binary at %q: %+v", location.RealPath, r)
}
}()
// Identify if bin was compiled by go
exes, err := openExe(reader)
exes, err = opener(reader)
if err != nil {
return nil, err
return pkgs, err
}
var pkgs []pkg.Package
for _, x := range exes {
goVersion, mod := findVers(x)
pkgs = append(pkgs, buildGoPkgInfo(location, mod, goVersion, x.ArchName())...)
}
return pkgs, nil
return pkgs, err
}
func buildGoPkgInfo(location source.Location, mod, goVersion, arch string) []pkg.Package {