You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
QuickJS RegExp duplicate named groups: uint8_t group_name_scope wraparound causes false SyntaxError at 257 alternatives #1591
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,
functionT(f){try{f();return"OK"}catch(e){return"ERR: "+e.message}}// 256 alts -> OK (control)vara=["(?<a>X)"];for(vari=0;i<254;i++)a.push("z");a.push("(?<a>Y)");print("256 alts : "+T(()=>newRegExp(a.join("|"))));// 257 alts -> FALSELY rejected (uint8 wraparound)varb=["(?<a>X)"];for(vari=0;i<255;i++)b.push("z");b.push("(?<a>Y)");print("257 alts : "+T(()=>newRegExp(b.join("|"))));// ERR: duplicate group name// 258 alts -> OK again (scope wrapped past 0)varc=["(?<a>X)"];for(vari=0;i<256;i++)c.push("z");c.push("(?<a>Y)");print("258 alts : "+T(()=>newRegExp(c.join("|"))));// ordinary duplicate-named-group usage works (control)vardn=newRegExp("(?:(?<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
Activity
Limit named group scopes to 256