Skip to content

QuickJS RegExp duplicate named groups: uint8_t group_name_scope wraparound causes false SyntaxError at 257 alternatives #1591

Closed

Description

@kid-lxy

QuickJS incorrectly rejects a valid RegExp pattern containing duplicate named capturing groups in disjoint alternatives when the number of alternatives reaches 257.

The issue appears to be caused by REParseState::group_name_scope being uint8_t. The parser increments group_name_scope for each alternative. At the 257th alternative, the scope wraps from 255 to 0, causing a group in branch 256 to be considered in the same scope as a group in branch 0. As a result, QuickJS throws "duplicate group name" even though the two groups cannot both participate in the same match.

When running the following code,

function T(f){try{f();return "OK"}catch(e){return "ERR: "+e.message}}

// 256 alts -> OK (control)
var a=["(?<a>X)"]; for(var i=0;i<254;i++) a.push("z"); a.push("(?<a>Y)");
print("256 alts  : " + T(()=>new RegExp(a.join("|"))));

// 257 alts -> FALSELY rejected (uint8 wraparound)
var b=["(?<a>X)"]; for(var i=0;i<255;i++) b.push("z"); b.push("(?<a>Y)");
print("257 alts  : " + T(()=>new RegExp(b.join("|"))));   // ERR: duplicate group name

// 258 alts -> OK again (scope wrapped past 0)
var c=["(?<a>X)"]; for(var i=0;i<256;i++) c.push("z"); c.push("(?<a>Y)");
print("258 alts  : " + T(()=>new RegExp(c.join("|"))));

// ordinary duplicate-named-group usage works (control)
var dn = new RegExp("(?:(?<a>x)|(?<a>y))\\k<a>");
print("dup yy: " + dn.exec("yy").groups.a + ", dup xx: " + dn.exec("xx").groups.a);

the output is as follows

256 alts  : OK
257 alts  : ERR: duplicate group name
258 alts  : OK
dup yy: y, dup xx: x

QuickJS version

QuickJS version/release: v0.15.1
Git commit: c8044be
Operating system: Ubuntu 24.04 x86_64

Activity

  1. added a commit that references this issue on Jul 20, 2026
    494c605
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions