Skip to content

Commit

Permalink
Fix invalid regexp serialization
Browse files Browse the repository at this point in the history
Invalid regexps were serialized as empty regexps, which match any expression, while invalid regexp should not match anything.
  • Loading branch information
poppolopoppo committed Dec 20, 2024
1 parent 1e94f60 commit 12c6db8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion internal/base/SerializableHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func NewRegexp(pattern string) Regexp {
}
func (x Regexp) Valid() bool { return x.Regexp != nil }
func (x *Regexp) Set(in string) (err error) {
x.Regexp, err = regexp.Compile(in)
if len(in) > 0 {
x.Regexp, err = regexp.Compile(in)
} else {
x.Regexp = nil
}
return
}
func (x Regexp) String() string {
Expand Down
12 changes: 10 additions & 2 deletions utils/UFS.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,13 @@ func MakeGlobRegexpExpr(glob ...string) string {
if len(glob) == 0 {
return ".*"
}

var expr strings.Builder
expr.WriteString("(?i)(?:") // insensitive, non-capturing group
expr.WriteString("(?i)") // insensitive
if len(glob) > 1 { // non-capturing group
expr.WriteString("(?:")
}

for i, it := range glob {
it = regexp.QuoteMeta(it)
it = strings.ReplaceAll(it, "\\?", ".")
Expand All @@ -1121,7 +1126,10 @@ func MakeGlobRegexpExpr(glob ...string) string {
expr.WriteString(it)
expr.WriteRune(')')
}
expr.WriteString(")")

if len(glob) > 1 { // non-capturing group
expr.WriteRune(')')
}
return expr.String()
}
func MakeGlobRegexp(glob ...string) base.Regexp {
Expand Down

0 comments on commit 12c6db8

Please sign in to comment.