From 07104f6e892dea1f39d10ab5d0937822186a6a43 Mon Sep 17 00:00:00 2001 From: thepetk Date: Fri, 26 May 2023 12:33:25 +0100 Subject: [PATCH] Add error handling for unmarshalling files Signed-off-by: thepetk --- .../enricher/framework/dotnet/dotnet_detector.go | 6 ++++-- .../framework/java/micronaut_detector.go | 5 ++++- .../framework/java/openliberty_detector.go | 5 ++++- .../enricher/framework/java/quarkus_detector.go | 5 ++++- .../enricher/framework/java/spring_detector.go | 5 ++++- go/pkg/utils/detector.go | 16 ++++++++++++---- go/pkg/utils/langfiles/languages_file_handler.go | 10 ++++++++-- 7 files changed, 40 insertions(+), 12 deletions(-) diff --git a/go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go b/go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go index 29916d04..61a099bc 100644 --- a/go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go +++ b/go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go @@ -63,8 +63,10 @@ func getFrameworks(configFilePath string) string { byteValue, _ := ioutil.ReadAll(xmlFile) var proj schema.DotNetProject - xml.Unmarshal(byteValue, &proj) - + err = xml.Unmarshal(byteValue, &proj) + if err != nil { + return "" + } defer func() error { if err := xmlFile.Close(); err != nil { return fmt.Errorf("error closing file: %s", err) diff --git a/go/pkg/apis/enricher/framework/java/micronaut_detector.go b/go/pkg/apis/enricher/framework/java/micronaut_detector.go index 071f21c1..94689bd6 100644 --- a/go/pkg/apis/enricher/framework/java/micronaut_detector.go +++ b/go/pkg/apis/enricher/framework/java/micronaut_detector.go @@ -76,7 +76,10 @@ func (m MicronautDetector) DoPortsDetection(component *model.Component, ctx *con func getMicronautPortsFromBytes(bytes []byte) []int { var ports []int var data MicronautApplicationProps - yaml.Unmarshal(bytes, &data) + err := yaml.Unmarshal(bytes, &data) + if err != nil { + return []int{} + } if data.Micronaut.Server.SSL.Enabled && utils.IsValidPort(data.Micronaut.Server.SSL.Port) { ports = append(ports, data.Micronaut.Server.SSL.Port) } diff --git a/go/pkg/apis/enricher/framework/java/openliberty_detector.go b/go/pkg/apis/enricher/framework/java/openliberty_detector.go index 0fb02ee8..e9f3648a 100644 --- a/go/pkg/apis/enricher/framework/java/openliberty_detector.go +++ b/go/pkg/apis/enricher/framework/java/openliberty_detector.go @@ -55,7 +55,10 @@ func (o OpenLibertyDetector) DoPortsDetection(component *model.Component, ctx *c return } var data ServerXml - xml.Unmarshal(bytes, &data) + err = xml.Unmarshal(bytes, &data) + if err != nil { + return + } ports := utils.GetValidPorts([]string{data.HttpEndpoint.HttpPort, data.HttpEndpoint.HttpsPort}) if len(ports) > 0 { component.Ports = ports diff --git a/go/pkg/apis/enricher/framework/java/quarkus_detector.go b/go/pkg/apis/enricher/framework/java/quarkus_detector.go index 97a48a54..b0303c39 100644 --- a/go/pkg/apis/enricher/framework/java/quarkus_detector.go +++ b/go/pkg/apis/enricher/framework/java/quarkus_detector.go @@ -140,7 +140,10 @@ func getServerPortsFromQuarkusApplicationYamlFile(file string) ([]int, error) { return []int{}, err } var data QuarkusApplicationYaml - yaml.Unmarshal(yamlFile, &data) + err = yaml.Unmarshal(yamlFile, &data) + if err != nil { + return []int{}, err + } var ports []int if data.Quarkus.Http.SSLPort > 0 { ports = append(ports, data.Quarkus.Http.SSLPort) diff --git a/go/pkg/apis/enricher/framework/java/spring_detector.go b/go/pkg/apis/enricher/framework/java/spring_detector.go index 23bb7065..448251e2 100644 --- a/go/pkg/apis/enricher/framework/java/spring_detector.go +++ b/go/pkg/apis/enricher/framework/java/spring_detector.go @@ -127,7 +127,10 @@ func getServerPortsFromYamlFile(file string) ([]int, error) { return []int{}, err } var data ApplicationProsServer - yaml.Unmarshal(yamlFile, &data) + err = yaml.Unmarshal(yamlFile, &data) + if err != nil { + return []int{}, err + } var ports []int if data.Server.Port > 0 { ports = append(ports, data.Server.Port) diff --git a/go/pkg/utils/detector.go b/go/pkg/utils/detector.go index 047cc043..00eece78 100644 --- a/go/pkg/utils/detector.go +++ b/go/pkg/utils/detector.go @@ -146,8 +146,10 @@ func GetPomFileContent(pomFilePath string) (schema.Pom, error) { byteValue, _ := ioutil.ReadAll(xmlFile) var pom schema.Pom - xml.Unmarshal(byteValue, &pom) - + err = xml.Unmarshal(byteValue, &pom) + if err != nil { + return schema.Pom{}, err + } defer func() error { if err := xmlFile.Close(); err != nil { return fmt.Errorf("error closing file: %s", err) @@ -191,7 +193,10 @@ func GetPackageJsonSchemaFromFile(path string) (schema.PackageJson, error) { } var packageJson schema.PackageJson - json.Unmarshal(bytes, &packageJson) + err = json.Unmarshal(bytes, &packageJson) + if err != nil { + return schema.PackageJson{}, err + } return packageJson, nil } @@ -217,7 +222,10 @@ func GetComposerJsonSchemaFromFile(path string) (schema.ComposerJson, error) { } var composerJson schema.ComposerJson - json.Unmarshal(bytes, &composerJson) + err = json.Unmarshal(bytes, &composerJson) + if err != nil { + return schema.ComposerJson{}, err + } return composerJson, nil } diff --git a/go/pkg/utils/langfiles/languages_file_handler.go b/go/pkg/utils/langfiles/languages_file_handler.go index b81a3062..49fb10e6 100644 --- a/go/pkg/utils/langfiles/languages_file_handler.go +++ b/go/pkg/utils/langfiles/languages_file_handler.go @@ -114,7 +114,10 @@ func getLanguagesProperties() schema.LanguagesProperties { return schema.LanguagesProperties{} } var data schema.LanguagesProperties - yaml.Unmarshal(yamlFile, &data) + err = yaml.Unmarshal(yamlFile, &data) + if err != nil { + return schema.LanguagesProperties{} + } return data } @@ -125,7 +128,10 @@ func getLanguageCustomizations() schema.LanguagesCustomizations { } var data schema.LanguagesCustomizations - yaml.Unmarshal(yamlFile, &data) + err = yaml.Unmarshal(yamlFile, &data) + if err != nil { + return schema.LanguagesCustomizations{} + } return data }