fix: certain inputs causing regex matching to hang #3500
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi there! I recently discovered an input file that causes UnoCSS to completely hang with 100% CPU usage, and after some investigation it seems to be due to this regex.
You can reproduce the issue by pasting the contents of that input file into https://unocss.dev/play/. The entire tab will freeze.
I went and updated the regexes here to replace all the unbounded
+
s and*
s with bounded{1,64}
s and{0,64}
s respectively, and added a test with the problematic input that now completes successfully in under 1s on my machine (previously the test will hang at 100% CPU as well).I chose 64 as the upper bound for all of these expressions for now, since it seems like a safe enough upper limit for the kinds of tokens UnoCSS looks for. Hoping tests will verify whether we need to increase it. Though we could probably drastically improve the worst-case performance of the regex by restricting specific pieces to lower limits. Will leave that to someone who understands the regex better than I do. 🙏