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

Question about line 148 in CssCompressor #228

Open
nhahtdh opened this issue Dec 18, 2015 · 0 comments
Open

Question about line 148 in CssCompressor #228

nhahtdh opened this issue Dec 18, 2015 · 0 comments

Comments

@nhahtdh
Copy link

nhahtdh commented Dec 18, 2015

The regex in this line:

p = Pattern.compile("(\"([^\\\\\"]|\\\\.|\\\\)*\")|(\'([^\\\\\']|\\\\.|\\\\)*\')");

seems to have caused some StackOverflowError. One case is reported in the OpenJDK bug tracker, but closed due to no repro, as the test case is too small to reveal the problem. There seems to be a similar issue here: #168.

I analyzed the StackOverflowError problem in detailed on StackOverflow. The workaround varies on case-by-case basis, but a common workarounds is to modify the regex to use possessive quantifier.

Back to the problem, from the comment, the pattern above is supposed to match a CSS string literal. However, I don't understand the last alternation in ([^\\\\\"]|\\\\.|\\\\)*. Wouldn't it allow an unclosed string like 'abcde\' from validating?

According to CSS specs, it seems that the correct regex should be:

"([^"\r\n\f\\]|\\[^0-9a-fA-f]|\\[0-9a-fA-F]{1,6}(?>\r\n|[ \t\r\n\f])?)*+"
|
'([^'\r\n\f\\]|\\[^0-9a-fA-f]|\\[0-9a-fA-F]{1,6}(?>\r\n|[ \t\r\n\f])?)*+'

Since the different alternations are mutually exclusive, it's possible to make the repetition possessive, as done above.

The code at line 148 should become:

p = Pattern.compile("\"([^\"\r\n\f\\\\]|\\\\[^0-9a-fA-f]|\\\\[0-9a-fA-F]{1,6}(?>\r\n|[ \t\r\n\f])?)*+\"|'([^'\r\n\f\\\\]|\\\\[^0-9a-fA-f]|\\\\[0-9a-fA-F]{1,6}(?>\r\n|[ \t\r\n\f])?)*+'");
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

No branches or pull requests

1 participant