Skip to content

Commit

Permalink
Merge pull request #783 from cloudflare/issue-782
Browse files Browse the repository at this point in the history
Fix false positive reports from promq/regexp
  • Loading branch information
prymitive authored Nov 17, 2023
2 parents 8e456d8 + b13f7b9 commit 373e392
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.49.2

### Fixed

- Fixed false positive reports from `promql/regexp` - #782.

## v0.49.1

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions internal/checks/promql_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ func (c RegexpCheck) Check(_ context.Context, _ string, rule parser.Rule, _ []di
var isUseful bool
var beginText, endText int
r, _ := syntax.Parse(re, syntax.Perl)
if r.Flags > 0 && r.Flags != syntax.Perl {
// If effective flags are different from default flags then we assume regexp is useful.
// It could be case sensitive match.
isUseful = true
}
for _, s := range r.Sub {
if s.Flags > 0 && s.Flags != syntax.Perl {
isUseful = true
}
// nolint: exhaustive
switch s.Op {
case syntax.OpBeginText:
Expand Down
7 changes: 7 additions & 0 deletions internal/checks/promql_regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func TestRegexpCheck(t *testing.T) {
}
},
},
{
description: "regexp with a modifier",
content: "- record: foo\n expr: foo{job=~\"(?i)someone\"}\n",
checker: newRegexpCheck,
prometheus: noProm,
problems: noProblems,
},
}
runTests(t, testCases)
}

0 comments on commit 373e392

Please sign in to comment.