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

Aperta
#624 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

@andreasrosdal ci sta già lavorando.

Dal 21/11/2026.

  • #625 di @andreasrosdal — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
c, javascript
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C
Stelle
11.1k
Fork
1.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bellard/quickjs

Tutte le issue di bellard/quickjs

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.