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

fix: certain inputs causing regex matching to hang #3500

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

lewisl9029
Copy link
Contributor

@lewisl9029 lewisl9029 commented Jan 9, 2024

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. 🙏

@lewisl9029 lewisl9029 requested a review from antfu as a code owner January 9, 2024 08:57
Copy link

netlify bot commented Jan 9, 2024

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d37c170
🔍 Latest deploy log https://app.netlify.com/sites/unocss/deploys/659d0a94826f870008855f5f
😎 Deploy Preview https://deploy-preview-3500--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@antfu antfu changed the title Fix certain inputs causing regex matching to hang fix: certain inputs causing regex matching to hang Jan 17, 2024
@antfu
Copy link
Member

antfu commented Jan 17, 2024

Thank you!

@antfu antfu added this pull request to the merge queue Jan 17, 2024
Merged via the queue into unocss:main with commit a361d77 Jan 17, 2024
9 checks passed
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.

None yet

2 participants