Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Handle errors from regexp #200

Merged
merged 7 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions go/pkg/apis/enricher/framework/go/go_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 8 additions & 2 deletions go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ func GetPortValuesFromEnvFile(root string, regexes []string) []int {
return ports
}
for _, regex := range regexes {
re := regexp.MustCompile(regex)
re, err := regexp.Compile(regex)
if err != nil {
continue
}
port := FindPortSubmatch(re, text, 1)
if port != -1 {
ports = append(ports, port)
Expand All @@ -420,7 +423,10 @@ func GetStringValueFromEnvFile(root string, regex string) string {
if err != nil {
return ""
}
re := regexp.MustCompile(regex)
re, err := regexp.Compile(regex)
if err != nil {
return ""
}
if text != "" {
matches := re.FindStringSubmatch(text)
if len(matches) > 1 {
Expand Down