-
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?
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 |
---|---|---|
|
@@ -11,8 +11,11 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import de.neuland.jade4j.exceptions.JadeCompilerException; | ||
import de.neuland.jade4j.filter.Filter; | ||
import de.neuland.jade4j.parser.node.MixinNode; | ||
import de.neuland.jade4j.parser.node.Node; | ||
import de.neuland.jade4j.template.JadeTemplate; | ||
|
||
public class JadeModel implements Map<String, Object> { | ||
|
||
|
@@ -22,6 +25,10 @@ public class JadeModel implements Map<String, Object> { | |
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 commentThe 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 commentThe 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. |
||
private Integer MAX_VISITED = 100; | ||
|
||
public JadeModel(Map<String, Object> defaults) { | ||
Map<String, Object> rootScope = new HashMap<String, Object>(); | ||
scopes.add(rootScope); | ||
|
@@ -50,6 +57,16 @@ public MixinNode getMixin(String name) { | |
return mixins.get(name); | ||
} | ||
|
||
public void visit(Node node, JadeTemplate template) { | ||
String visitId = node.getFileName()+":"+node.getLineNumber(); | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
visited.put(visitId, ct); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
scopes.clear(); | ||
|
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?