Skip to content

Commit

Permalink
Suppress further logging of bad regex (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
scudette authored Feb 6, 2025
1 parent 1215b6d commit 35c537c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions protocols/protocol_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ func Match(scope types.Scope, pattern string, target string) bool {
re_any, pres := scope.GetContext(key)
if pres {
re, _ = re_any.(*regexp.Regexp)
if re == nil {
return false
}

} else {
var err error
re, err = regexp.Compile("(?i)" + pattern)
if err != nil {
scope.Log("Compile regexp: %v", err)
// Cache the error to avoid logging again
scope.SetContext(key, nil)

return false
}

Expand Down
7 changes: 7 additions & 0 deletions stored.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package vfilter

import (
"context"
"fmt"

"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter/types"
Expand All @@ -45,6 +46,12 @@ func NewStoredQuery(query *_Select, name string) *_StoredQuery {
}
}

func (self *_StoredQuery) GoString() string {
scope := NewScope()
return fmt.Sprintf("StoredQuery{name: %v, query: {%v}, parameters: %v}",
self.name, FormatToString(scope, self.query), self.parameters)
}

func (self *_StoredQuery) Eval(ctx context.Context, scope types.Scope) <-chan Row {
output_chan := make(chan Row)

Expand Down
26 changes: 26 additions & 0 deletions types/frozen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

import (
"context"
)

// A FrozenStoredQuery is a stored query which will be evaluated
// inside the defined scope instead of the calling scope.
type FrozenStoredQuery struct {
query StoredQuery
defined_scope Scope
}

func (self FrozenStoredQuery) Query() StoredQuery {
return self.query
}

func (self FrozenStoredQuery) Eval(
ctx context.Context, scope Scope) <-chan Row {
return self.query.Eval(ctx, self.defined_scope)
}

func NewFrozenStoredQuery(
query StoredQuery, scope Scope) StoredQuery {
return &FrozenStoredQuery{query: query, defined_scope: scope}
}
5 changes: 5 additions & 0 deletions vfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ type _Select struct {
Limit *int64 `[ LIMIT @Number ]`
}

func (self *_Select) GoString() string {
scope := NewScope()
return FormatToString(scope, self)
}

func (self *_Select) Eval(ctx context.Context, scope types.Scope) <-chan Row {
// If the EXPLAIN keyword was used, enabled explaining for this
// scope and its children.
Expand Down
3 changes: 3 additions & 0 deletions visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func (self *Visitor) Visit(node interface{}) {
self.line_break()
}

case *types.FrozenStoredQuery:
self.Visit(t.Query())

case *_StoredQuery:
self.Visit(t.query)

Expand Down

0 comments on commit 35c537c

Please sign in to comment.