From d1f7ca423959a7ba0d8d535f1ad121c8f24cd784 Mon Sep 17 00:00:00 2001 From: Katharina Dankert Date: Thu, 22 Feb 2018 23:44:09 +0100 Subject: [PATCH 1/2] Fix the test for bad regexes This test did not fail because the regex was bad, it failed because yaml parsing failed. This fixes the yaml which uncovers a panic in initFromYAMLString. --- mapper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mapper_test.go b/mapper_test.go index 5803fb7..3605e3d 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -310,7 +310,7 @@ mappings: { config: `--- mappings: -- match: "*\.foo" +- match: "*\\.foo" match_type: regex name: "foo" labels: {} From 7e86bcaa922a495f11f6f748a2c32d462516113f Mon Sep 17 00:00:00 2001 From: Katharina Dankert Date: Thu, 22 Feb 2018 23:15:30 +0100 Subject: [PATCH 2/2] Avoid panic from compiling user supplied regex If an invalid regex is supplied mustCompile would panic during configuration reload. Return an error instead that can be handled. --- mapper.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mapper.go b/mapper.go index 980daa3..55d6f3b 100644 --- a/mapper.go +++ b/mapper.go @@ -106,9 +106,17 @@ func (m *metricMapper) initFromYAMLString(fileContents string) error { // can use to match metrics later on. metricRe := strings.Replace(currentMapping.Match, ".", "\\.", -1) metricRe = strings.Replace(metricRe, "*", "([^.]*)", -1) - currentMapping.regex = regexp.MustCompile("^" + metricRe + "$") + if regex, err := regexp.Compile("^" + metricRe + "$"); err != nil { + return fmt.Errorf("invalid match %s. cannot compile regex in mapping: %v", currentMapping.Match, err) + } else { + currentMapping.regex = regex + } } else { - currentMapping.regex = regexp.MustCompile(currentMapping.Match) + if regex, err := regexp.Compile(currentMapping.Match); err != nil { + return fmt.Errorf("invalid regex %s in mapping: %v", currentMapping.Match, err) + } else { + currentMapping.regex = regex + } } if currentMapping.TimerType == "" {