fix: stop panicking on "devel" version go stdlib (#3043)

Previously, if a Go binary was cataloged with build info indicating that
the go compiler version used was "deve", syft would panic on a nil
pointer dereference. Instead, skip creating a Go stdlib reference and
relationship for such a package.

Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
William Murphy 2024-07-16 09:51:14 -04:00 committed by GitHub
parent 278b72d39b
commit 75902b0540
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View File

@ -33,10 +33,11 @@ func stdlibPackageAndRelationships(pkgs []pkg.Package) ([]pkg.Package, []artifac
} }
stdLibPkg := newGoStdLib(mValue.GoCompiledVersion, goPkg.Locations) stdLibPkg := newGoStdLib(mValue.GoCompiledVersion, goPkg.Locations)
if stdLibPkg != nil { if stdLibPkg == nil {
goCompilerPkgs = append(goCompilerPkgs, *stdLibPkg) continue
totalLocations.Add(location)
} }
goCompilerPkgs = append(goCompilerPkgs, *stdLibPkg)
totalLocations.Add(location)
relationships = append(relationships, artifact.Relationship{ relationships = append(relationships, artifact.Relationship{
From: *stdLibPkg, From: *stdLibPkg,

View File

@ -68,6 +68,22 @@ func Test_stdlibPackageAndRelationships(t *testing.T) {
wantPkgs: 1, wantPkgs: 1,
wantRels: 1, wantRels: 1,
}, },
{
name: "go binary package with devel stdlib",
pkgs: []pkg.Package{
{
Name: "github.com/something/go",
Version: "1.0.0",
Locations: file.NewLocationSet(file.NewLocation("/bin/my-app")),
Metadata: pkg.GolangBinaryBuildinfoEntry{
GoCompiledVersion: "devel",
MainModule: "github.com/something/go",
},
},
},
wantPkgs: 0,
wantRels: 0,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {