baum1982/java-prettify

java.lang.StackOverflowError with long lines with escapes

Opened this issue · 0 comments

What steps will reproduce the problem?
1. find a source code with long strings with escapes (unfortunately way too 
common in large codebases)
2. try to use the prettifier on them
3. watch StackOverflowError

- What is the expected output? What do you see instead?
The expected output is a prettified output. Instead, one gets an application 
crash if StackOverflowError is not handled.

- What version of the product are you using? On what operating system?
Both the error and the proposed patch were observed and tested with java 
1.7.0_17 under windows and linux.

- Please provide any additional information below.
The issue seem to be that java regex implementation implements the choices 
using recursive calls. Thus, patterns such as (a|b)* when a abd b are sinle 
characters result in a lot of stack use if the engine cannot determine a and b 
are mutually exclusive (and it does not in most situations).
An example of such failure can be observed by prettifying the ltmain.sh file 
from libtool, which is present in most projects, and contains large shell 
multiline strings. A copy can be found at
http://svn.r-project.org/R/trunk/tools/ltmain.sh
Similar issue has been observed for java/cpp strings, and python triple quoted 
strings
(for instance 
https://raw.github.com/numpy/numpy/master/numpy/lib/tests/test_format.py).

The attached patch tries to avoid such issue, by rewriting the original regexes 
for strings to remove most choices operators.
This patch has been tested on a very large codebase, with different languages.

Best regards,
antoine

Original issue reported on code.google.com by areil...@gmail.com on 23 Nov 2013 at 2:39

Attachments: