RegExp: patterns with more than 255 capture groups fail with "too many captures"
Maintainers usually reply within 5 days
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- c, javascript
- Domain
- compilers
Research direction
Start in libregexp.c at CAPTURE_COUNT_MAX and the opcodes save_start, save_end, save_reset and the back-reference ops, plus the RE_HEADER_CAPTURE_COUNT header field. Widening the capture index to 16 bits touches the bytecode format, so check every reader and writer. Done when the 300-group repro in the issue compiles and back-references to groups past 255 still match. Pull request #625 already covers this, so coordinate there first.
Written by the indexing model from the issue text.
Description
libregexp.c limits a regular expression to 255 capture groups (CAPTURE_COUNT_MAX 255); a pattern with more fails to compile with SyntaxError: too many captures. ECMAScript sets no such limit, and V8, SpiderMonkey and JavaScriptCore accept patterns with thousands of groups.
Reproduction
let src = "";
for (let i = 0; i < 300; i++) src += "(a)?";
new RegExp(src); // SyntaxError: too many captures
Where it shows up
This is not only a synthetic case. Bundlers generate such patterns: rspack's CSS chunk loader tests chunk ids with one large alternation in which every branch is a capture group, e.g.
/^(4(0(1(071|295|317|425)|074|271|3655|...)|...)|...)$/.test(chunkId)
A large web application (Discord's web client) ships an entry bundle with more than 255 groups in that test, so the whole bundle fails to parse and the application never starts.
Cause
The capture index is a single byte throughout: the operand of save_start/save_end, both operands of save_reset, the back-reference count and indices, and the capture count in the bytecode header (RE_HEADER_CAPTURE_COUNT).
Possible fix
Widening these to 16 bits removes the limit in practice (65535 groups) without changing the matcher: save_start/save_end take a u16 operand, save_reset two, back-references a u16 count followed by u16 indices, and the header stores the capture count as a u16 (the header grows from 8 to 10 bytes). Capture storage is already heap-allocated by the callers from lre_get_alloc_count(), so a larger count only costs memory for patterns that actually use it. We made this change in a downstream fork and it passes the regexp tests we run, plus back-references and named back-references to groups past 255.
- Dominant language
- C
- Stars
- 11.1k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bellard/quickjs
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bellard/quickjs#620 · 1 comment ·
Maintainers usually reply within 5 days
-
(comment on #611) `function g(a, d = (a = 10)) { return a; }` returns the argument instead of 10Open
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
bellard/quickjs#611 · 1 comment ·
Maintainers usually reply within 5 days
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
FujiNetWIFI/fujinet-firmware#2518 ·
Maintainers usually reply within 1 day
-
category:port-update
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 2 days
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
lauppv/cyberstars#610 ·
Maintainers usually reply within 1 day
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100