Skip to content

regexp: allow up to 65535 capture groups - #625

Open
andreasrosdal wants to merge 1 commit into
bellard:masterfrom
andreasrosdal:regexp-capture-count-u16
Open

andreasrosdal wants to merge 1 commit into
bellard:masterfrom
andreasrosdal:regexp-capture-count-u16

Conversation

@andreasrosdal

Copy link
Copy Markdown

Fixes #624.

libregexp.c stored capture indices on 8 bits, so a pattern with more than 255 capture groups failed with SyntaxError: too many captures. Bundlers emit such patterns (e.g. rspack's CSS chunk test is one large alternation of capture groups), so real applications fail to load.

This widens the capture index to 16 bits and raises CAPTURE_COUNT_MAX to 65535:

  • save_start / save_end take a 16-bit index, save_reset two 16-bit indices
  • the back reference opcodes take a 16-bit count followed by 16-bit indices
  • the capture count in the bytecode header is 16 bits, so the header grows from 8 to 10 bytes

The register count is unchanged. Since the regexp bytecode layout changes, previously serialized bytecode containing regexps is not compatible; I did not bump BC_VERSION, tell me if you want that included.

Tests added to tests/test_builtin.js (300 groups with numbered and named back references, and save_reset inside a quantifier). make test passes, and libregexp.c still builds with DUMP_REOP.

Capture indices were stored on 8 bits, limiting a regexp to 255 capture
groups. Store them on 16 bits in the save_start, save_end, save_reset
and back reference opcodes and in the bytecode header.
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.

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

1 participant