-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change recursion checks to be rule based, better match the spec #443
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ func NewParser(opts ...Option) (*Parser, error) { | |
} | ||
} | ||
if p.maxRecursionDepth == 0 { | ||
p.maxRecursionDepth = 250 | ||
p.maxRecursionDepth = 200 | ||
} | ||
if p.maxRecursionDepth == -1 { | ||
p.maxRecursionDepth = int((^uint(0)) >> 1) | ||
|
@@ -158,29 +158,41 @@ func (re *recursionError) Error() string { | |
var _ error = &recursionError{} | ||
|
||
type recursionListener struct { | ||
maxDepth int | ||
depth int | ||
maxDepth int | ||
ruleTypeDepth map[int]*int | ||
} | ||
|
||
func (rl *recursionListener) VisitTerminal(node antlr.TerminalNode) {} | ||
|
||
func (rl *recursionListener) VisitErrorNode(node antlr.ErrorNode) {} | ||
|
||
func (rl *recursionListener) EnterEveryRule(ctx antlr.ParserRuleContext) { | ||
if ctx != nil && ctx.GetRuleIndex() == gen.CELParserRULE_expr { | ||
if rl.depth >= rl.maxDepth { | ||
rl.depth++ | ||
panic(&recursionError{ | ||
message: fmt.Sprintf("expression recursion limit exceeded: %d", rl.maxDepth), | ||
}) | ||
} | ||
rl.depth++ | ||
if ctx == nil { | ||
return | ||
} | ||
ruleIndex := ctx.GetRuleIndex() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is safe. I remember having to put the increment after the recursion check. But looking at ANTLR Go, it appears that if EnterEveryRule panics, ExitEveryRule should not be called. Might want to double-check, otherwise things could go negative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added a defensive check since I'm using a pointer now as well. |
||
depth, found := rl.ruleTypeDepth[ruleIndex] | ||
if !found { | ||
var counter = 1 | ||
rl.ruleTypeDepth[ruleIndex] = &counter | ||
depth = &counter | ||
} else { | ||
*depth++ | ||
} | ||
if *depth >= rl.maxDepth { | ||
panic(&recursionError{ | ||
message: fmt.Sprintf("expression recursion limit exceeded: %d", rl.maxDepth), | ||
}) | ||
} | ||
} | ||
|
||
func (rl *recursionListener) ExitEveryRule(ctx antlr.ParserRuleContext) { | ||
if ctx != nil && ctx.GetRuleIndex() == gen.CELParserRULE_expr { | ||
rl.depth-- | ||
if ctx == nil { | ||
return | ||
} | ||
ruleIndex := ctx.GetRuleIndex() | ||
if depth, found := rl.ruleTypeDepth[ruleIndex]; found && *depth > 0 { | ||
*depth-- | ||
} | ||
} | ||
|
||
|
@@ -214,7 +226,7 @@ func (rl *recoveryLimitErrorStrategy) RecoverInline(recognizer antlr.Parser) ant | |
} | ||
|
||
func (rl *recoveryLimitErrorStrategy) checkAttempts(recognizer antlr.Parser) { | ||
if rl.attempts >= rl.maxAttempts { | ||
if rl.attempts == rl.maxAttempts { | ||
rl.attempts++ | ||
msg := fmt.Sprintf("error recovery attempt limit exceeded: %d", rl.maxAttempts) | ||
recognizer.NotifyErrorListeners(msg, nil, nil) | ||
|
@@ -263,7 +275,8 @@ func (p *parser) parse(expr runes.Buffer, desc string) *exprpb.Expr { | |
// Unfortunately ANTLR Go runtime is missing (*antlr.BaseParser).RemoveParseListeners, so this is | ||
// good enough until that is exported. | ||
prsrListener := &recursionListener{ | ||
maxDepth: p.maxRecursionDepth, | ||
maxDepth: p.maxRecursionDepth, | ||
ruleTypeDepth: map[int]*int{}, | ||
} | ||
|
||
defer func() { | ||
|
@@ -295,7 +308,7 @@ func (p *parser) parse(expr runes.Buffer, desc string) *exprpb.Expr { | |
case *recursionError: | ||
p.errors.ReportError(common.NoLocation, "%s", err.message) | ||
case *recoveryLimitError: | ||
p.errors.ReportError(common.NoLocation, "%s", err.message) | ||
// do nothing, listeners already notified and error reported. | ||
default: | ||
panic(val) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speed-wise, it might make more sense to use map[int]*int. That way you do not need to do do both a lookup and insert into a map each time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done