RegExp: patterns with more than 255 capture groups fail with "too many captures"

Open
#624 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

@andreasrosdal is already working on this.

Since Nov 21, 2026.

  • #625 by @andreasrosdal — open

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bellard/quickjs

All issues in bellard/quickjs

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.