Skip to content
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

Merged
merged 2 commits into from
Aug 10, 2021

Conversation

TristonianJones
Copy link
Collaborator

The CEL spec indicates that the max recursion limit for any one grammar element
is effectively 32. At present, the current recursion limiting strategy employed by
the parser only checks for recursion at the top-level expr node, but recursion can
also occur within subrules like calc, member, etc. The new strategy enforces
the configured limit on a per-rule basis, rather than just for expr to account for
recursion in subrules.

if ctx == nil {
return
}
ruleIndex := ctx.GetRuleIndex()
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if ctx == nil {
return
}
ruleIndex := ctx.GetRuleIndex()
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

@TristonianJones TristonianJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, note that I added a check to validate that error recovery limits were being tested properly as well. I made a minor code change to prevent double-reporting of error recovery issues as a result of the test.

if ctx == nil {
return
}
ruleIndex := ctx.GetRuleIndex()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

parser/parser.go Outdated Show resolved Hide resolved
if ctx == nil {
return
}
ruleIndex := ctx.GetRuleIndex()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@TristonianJones
Copy link
Collaborator Author

PTAL

@jcking jcking self-requested a review August 9, 2021 23:52
@TristonianJones TristonianJones merged commit 2457df3 into google:master Aug 10, 2021
@TristonianJones TristonianJones deleted the parser-recursion-fix branch August 10, 2021 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants