I give to user a possibility to execute regex over a lot of texts on my Java server. It's implemented with Pattern
and Matcher
Java classes.
In some cases it leads to StackOverflowError
. It happens for complex regexps and/or when a lot of matches occures.
Pattern pattern = Pattern.compile(term);
Matcher matcher = pattern.matcher(text);
java.lang.StackOverflowError: null
at java.util.regex.Pattern$Branch.match(Pattern.java:4604) ~[na:1.8.0_45-internal]
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658) ~[na:1.8.0_45-internal]
...
The question is how can I limit users to use only a small subset of regexps to avoid such errors and unwanted high server's CPU usage. Can it be done with pure Java or any third-party Java library?
Or maybe there is other possible solutions, i.e. just try/catch StackOverflowError
?