lib/logstorage: avoid panic when parsing regex with stream filter (#5897)

This commit is contained in:
XLONG96 2024-02-29 21:31:54 +08:00 committed by GitHub
parent f2266f40b7
commit a5795f533d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 13 deletions

View file

@ -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
}

View file

@ -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)
}