From 88b9088499c38484acc13b77792c42be8158c173 Mon Sep 17 00:00:00 2001 From: XLONG96 <1073501156@qq.com> Date: Thu, 29 Feb 2024 21:31:54 +0800 Subject: [PATCH] lib/logstorage: avoid panic when parsing regex with stream filter (#5897) --- lib/logstorage/parser.go | 8 ++++++++ lib/logstorage/stream_filter.go | 14 +------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/logstorage/parser.go b/lib/logstorage/parser.go index d0f66c898e..104491a018 100644 --- a/lib/logstorage/parser.go +++ b/lib/logstorage/parser.go @@ -12,6 +12,7 @@ import ( "unicode/utf8" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil" ) type lexer struct { @@ -1070,6 +1071,13 @@ func parseStreamTagFilter(lex *lexer) (*streamTagFilter, error) { op: op, value: value, } + if op == "=~" || op == "!~" { + re, err := regexutil.NewPromRegex(value) + if err != nil { + return nil, fmt.Errorf("invalid regexp %q for stream filter: %w", value, err) + } + stf.regexp = re + } return stf, nil } diff --git a/lib/logstorage/stream_filter.go b/lib/logstorage/stream_filter.go index 838e95ed03..0cce172295 100644 --- a/lib/logstorage/stream_filter.go +++ b/lib/logstorage/stream_filter.go @@ -3,11 +3,9 @@ package logstorage import ( "strconv" "strings" - "sync" "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" - "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil" ) @@ -68,23 +66,13 @@ type streamTagFilter struct { // value is the value value string - regexpOnce sync.Once - regexp *regexutil.PromRegex + regexp *regexutil.PromRegex } func (tf *streamTagFilter) getRegexp() *regexutil.PromRegex { - tf.regexpOnce.Do(tf.initRegexp) return tf.regexp } -func (tf *streamTagFilter) initRegexp() { - re, err := regexutil.NewPromRegex(tf.value) - if err != nil { - logger.Panicf("BUG: cannot parse regexp %q: %s", tf.value, err) - } - tf.regexp = re -} - func (tf *streamTagFilter) String() string { return quoteTokenIfNeeded(tf.tagName) + tf.op + strconv.Quote(tf.value) }