-
Notifications
You must be signed in to change notification settings - Fork 1
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
Detect infinite loop/crash #5
base: master
Are you sure you want to change the base?
Detect infinite loop/crash #5
Conversation
7c17b4c
to
88b06dd
Compare
@@ -22,6 +25,10 @@ | |||
private Map<String, MixinNode> mixins = new HashMap<String, MixinNode>(); | |||
private Map<String, Filter> filter = new HashMap<String, Filter>(); | |||
|
|||
// to detect infinite loop | |||
private Map<String, Integer> visited = new HashMap<String, Integer>(); |
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.
Are you sure this class (JadeModel) and therefore this Map will be newly instantiated every time we parse a different document?
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.
Could add a unit test to prove this by parsing the same document multiple times.
@@ -22,6 +25,10 @@ | |||
private Map<String, MixinNode> mixins = new HashMap<String, MixinNode>(); | |||
private Map<String, Filter> filter = new HashMap<String, Filter>(); | |||
|
|||
// to detect infinite loop |
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.
Could we also add a comment with the cases we've seen leading to infinite loop?
Integer ct = visited.getOrDefault(visitId, 0) +1; | ||
if (ct > MAX_VISITED) { | ||
String error = "Potential infinite loop: "+visitId+" has been visited more then "+MAX_VISITED+" times!"; | ||
throw new JadeCompilerException(node, template.getTemplateLoader(), error); |
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.
Can we get the Template Name somehow and return it in the Exception message?
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.
visitId
contains file path and line number
Can you add a unit test validating this throws the right error upon infinite loop? Could add a couple of cases that we've identified from the jade templates we've made (Eg. crash.jade, etc) |
Currently when we have templates include each other and using
block
improperly, there is a chance jade4j got infinite loop results in StackOverflowError, this will crash the process. This PR counts how many time each node being visited and if the count for specific node reach the threshold (currently set to 100) we fail the compile instead of crash.