From 050e225d07453034363144a7c5396ce31f1546bd Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 1 Aug 2022 13:00:10 +0200 Subject: [PATCH] Fix type assertions from directory_url expression result --- cmd/csaf_checker/processor.go | 2 +- csaf/advisories.go | 2 +- util/json.go | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/csaf_checker/processor.go b/cmd/csaf_checker/processor.go index 49b134b..14cd6de 100644 --- a/cmd/csaf_checker/processor.go +++ b/cmd/csaf_checker/processor.go @@ -918,7 +918,7 @@ func (p *processor) checkCSAFs(domain string) error { p.badProviderMetadata.warn("extracting directory URLs failed: %v.", err) } else { var ok bool - dirURLs, ok = directoryURLs.([]string) + dirURLs, ok = util.AsStrings(directoryURLs) if !ok { p.badProviderMetadata.warn("directory URLs are not strings.") } diff --git a/csaf/advisories.go b/csaf/advisories.go index 84be1d9..66f713b 100644 --- a/csaf/advisories.go +++ b/csaf/advisories.go @@ -153,7 +153,7 @@ func (afp *AdvisoryFileProcessor) Process( lg("extracting directory URLs failed: %v\n", err) } else { var ok bool - dirURLs, ok = directoryURLs.([]string) + dirURLs, ok = util.AsStrings(directoryURLs) if !ok { lg("directory_urls are not strings.\n") } diff --git a/util/json.go b/util/json.go index bd53f64..6f2288b 100644 --- a/util/json.go +++ b/util/json.go @@ -166,3 +166,19 @@ func (pe *PathEval) Strings( } return results, nil } + +// AsStrings transforms an []interface{string, string,... } +// to a []string. +func AsStrings(x interface{}) ([]string, bool) { + strs, ok := x.([]interface{}) + if !ok { + return nil, false + } + res := make([]string, 0, len(strs)) + for _, y := range strs { + if s, ok := y.(string); ok { + res = append(res, s) + } + } + return res, true +}