nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.65k stars 29.62k forks source link

Specific RegEx expression and string causes Node interpreter to hang indefinitely #34079

Closed transiient closed 4 years ago

transiient commented 4 years ago

What steps will reproduce the bug?

  1. Start the Node interpreter (> node)
  2. Paste this Pastebin content into a new string variable - let or const, doesn't matter. Use backticks as it's a multi-line string
  3. Run console.log(string.match(/\]\(*(.*?)*\)/gi) || []);
  4. Observe the hanging

How often does it reproduce? Is there a required condition?

Every time you run that sequence of commands with that particular string. I don't know why.

What is the expected behavior?

Output of matched expressions in the string, as shown on Regexr with the exact same string and RegEx expression.

What do you see instead?

image

Additional information

I was documenting this issue here originally, as I first suspected an issue in Next.js: https://github.com/vercel/next.js/issues/14611 After many hours of debugging, I figured out the issue lies in Node itself.

transiient commented 4 years ago

I just tested this in the Firefox dev console and it even happens there. Woah. Looks like this is a much bigger than issue than I originally thought. That means both SpiderMonkey and V8 have this bug.

devsnek commented 4 years ago

You may find this illuminating: https://javascript.info/regexp-catastrophic-backtracking

transiient commented 4 years ago

@devsnek thanks for that article - I guess that's the issue! Only saw your response after I'd filed bug reports in both the Mozilla and Chromium trackers... whoops. Still valid in both though.

bnoordhuis commented 4 years ago

I'm closing this as it's not a Node.js issue. Note that it's a longstanding issue and not considered a bug by the JS engines as such (and they have a point - it's a bug in the regular expression, the engine just does what it's told.)