From 05f073f42f8e93f93c456d30bef266c1d12cdffa Mon Sep 17 00:00:00 2001 From: gotjosh Date: Tue, 28 Jun 2022 11:51:06 +0100 Subject: [PATCH 1/2] Ensure matcher values are present when parsing matchers from strings Fixes #2936 Signed-off-by: gotjosh --- pkg/labels/parse.go | 6 +++++- pkg/labels/parse_test.go | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/labels/parse.go b/pkg/labels/parse.go index 0a242d506f..d0978ff6b8 100644 --- a/pkg/labels/parse.go +++ b/pkg/labels/parse.go @@ -120,8 +120,12 @@ func ParseMatcher(s string) (_ *Matcher, err error) { return nil, errors.Errorf("bad matcher format: %s", s) } + rawValue := ms[3] + if len(rawValue) == 0 { + return nil, errors.Errorf("matcher value is not present: %s", s) + } + var ( - rawValue = ms[3] value strings.Builder escaped bool expectTrailingQuote bool diff --git a/pkg/labels/parse_test.go b/pkg/labels/parse_test.go index c15541b87f..b1f53ef2fc 100644 --- a/pkg/labels/parse_test.go +++ b/pkg/labels/parse_test.go @@ -189,6 +189,10 @@ func TestMatchers(t *testing.T) { return append(ms, m, m2) }(), }, + { + input: `job=`, + err: `matcher value is not present: job=`, + }, { input: `job="value`, err: `matcher value contains unescaped double quote: "value`, From bd89550ce28df532270297ac459ce598eee5ce18 Mon Sep 17 00:00:00 2001 From: gotjosh Date: Fri, 30 Sep 2022 11:51:17 +0100 Subject: [PATCH 2/2] Take another approach Signed-off-by: gotjosh --- pkg/labels/parse.go | 8 ++------ pkg/labels/parse_test.go | 5 ++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/labels/parse.go b/pkg/labels/parse.go index d0978ff6b8..06a414e77d 100644 --- a/pkg/labels/parse.go +++ b/pkg/labels/parse.go @@ -120,18 +120,14 @@ func ParseMatcher(s string) (_ *Matcher, err error) { return nil, errors.Errorf("bad matcher format: %s", s) } - rawValue := ms[3] - if len(rawValue) == 0 { - return nil, errors.Errorf("matcher value is not present: %s", s) - } - var ( + rawValue = ms[3] value strings.Builder escaped bool expectTrailingQuote bool ) - if rawValue[0] == '"' { + if strings.HasPrefix(rawValue, "\"") { rawValue = strings.TrimPrefix(rawValue, "\"") expectTrailingQuote = true } diff --git a/pkg/labels/parse_test.go b/pkg/labels/parse_test.go index b1f53ef2fc..e1203f4a1a 100644 --- a/pkg/labels/parse_test.go +++ b/pkg/labels/parse_test.go @@ -191,7 +191,10 @@ func TestMatchers(t *testing.T) { }, { input: `job=`, - err: `matcher value is not present: job=`, + want: func() []*Matcher { + m, _ := NewMatcher(MatchEqual, "job", "") + return []*Matcher{m} + }(), }, { input: `job="value`,