From e48ae9eaacc00680dfa7cb8f2e8f37a5ee66c974 Mon Sep 17 00:00:00 2001 From: thepetk Date: Thu, 4 May 2023 13:12:50 +0100 Subject: [PATCH] Replace MustCompile in GetPortWithMatchIndexesGo with Compile and catch errors Signed-off-by: thepetk --- go/pkg/apis/enricher/framework/go/go_detector.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/go/pkg/apis/enricher/framework/go/go_detector.go b/go/pkg/apis/enricher/framework/go/go_detector.go index 40b142cb..e957b45b 100644 --- a/go/pkg/apis/enricher/framework/go/go_detector.go +++ b/go/pkg/apis/enricher/framework/go/go_detector.go @@ -103,19 +103,28 @@ func GetPortWithMatchIndexesGo(content string, matchIndexes []int, toBeReplaced //we should end up with something like ".ListenAndServe(PORT" portPlaceholder = strings.Replace(portPlaceholder, toBeReplaced, "", -1) // if we are lucky enough portPlaceholder contains a real HOST:PORT otherwise it is a variable/expression - re := regexp.MustCompile(`:*(\d+)`) + re, err := regexp.Compile(`:*(\d+)`) + if err != nil { + return -1 + } if port := utils.FindPortSubmatch(re, portPlaceholder, 1); port != -1 { return port } // we are not dealing with a host:port, let's try to find a variable set before the listen function contentBeforeMatch := content[0:matchIndexes[0]] - re = regexp.MustCompile(portPlaceholder + `\s+[:=]+\s"([^"]*)`) + re, err = regexp.Compile(portPlaceholder + `\s+[:=]+\s"([^"]*)`) + if err != nil { + return -1 + } matches := re.FindStringSubmatch(contentBeforeMatch) if len(matches) > 0 { // hostPortValue should be host:port hostPortValue := matches[len(matches)-1] - re = regexp.MustCompile(`:*(\d+)$`) + re, err = regexp.Compile(`:*(\d+)$`) + if err != nil { + return -1 + } if port := utils.FindPortSubmatch(re, hostPortValue, 1); port != -1 { return port }