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

Detect infinite loop/crash #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ychai-coursera
Copy link

@ychai-coursera ychai-coursera commented Feb 9, 2021

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.

@ychai-coursera ychai-coursera force-pushed the ychai/fix_template_loop branch from 7c17b4c to 88b06dd Compare February 9, 2021 18:46
@@ -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>();

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?

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

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);

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?

Copy link
Author

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

@stanley-coursera
Copy link

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)

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