Skip to content

Commit

Permalink
fix: stop some log.Warn spam due parsing an empty string as a CPE (#3330
Browse files Browse the repository at this point in the history
)

* chore: don't try to parse empty string as CPE

Signed-off-by: Will Murphy <[email protected]>

* chore: improve OS name and version extraction from ELF metadata

Signed-off-by: Will Murphy <[email protected]>

---------

Signed-off-by: Will Murphy <[email protected]>
  • Loading branch information
willmurphyscode authored Oct 15, 2024
1 parent 138c6e3 commit 754cebe
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions syft/pkg/cataloger/binary/elf_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,7 @@ func newELFPackage(metadata elfBinaryPackageNotes, locations file.LocationSet) p
func packageURL(metadata elfBinaryPackageNotes) string {
var qualifiers []packageurl.Qualifier

os := metadata.OS
osVersion := metadata.OSVersion

var atts cpe.Attributes
atts, err := cpe.NewAttributes(metadata.OSCPE)
if err != nil {
log.WithFields("error", err).Warn("unable to parse cpe attributes for elf binary package")
}
// only "upgrade" the OS information if there is something more specific to use in it's place
if os == "" && osVersion == "" || os == "" && atts.Version != "" || atts.Product != "" && osVersion == "" {
os = atts.Product
osVersion = atts.Version
}
os, osVersion := osNameAndVersionFromMetadata(metadata)

if os != "" {
osQualifier := os
Expand Down Expand Up @@ -70,6 +58,26 @@ func packageURL(metadata elfBinaryPackageNotes) string {
).ToString()
}

func osNameAndVersionFromMetadata(metadata elfBinaryPackageNotes) (string, string) {
os := metadata.OS
osVersion := metadata.OSVersion

if os != "" && osVersion != "" {
return os, osVersion
}

if metadata.OSCPE == "" {
return "", ""
}

attrs, err := cpe.NewAttributes(metadata.OSCPE)
if err != nil {
log.WithFields("error", err).Trace("unable to parse cpe attributes for elf binary package")
return "", ""
}
return attrs.Product, attrs.Version
}

const alpmType = "alpm"

func purlDistroType(ty string) string {
Expand Down

0 comments on commit 754cebe

Please sign in to comment.