Skip to content

Regex: Fix UTF-16 encoding of U+10000 in named capture groups - #2340

Open
asxxb wants to merge 1 commit into
meta:static_hfrom
asxxb:fix/regex-u10000-group-name
Open

asxxb wants to merge 1 commit into
meta:static_hfrom
asxxb:fix/regex-u10000-group-name

Conversation

@asxxb

@asxxb asxxb commented Nov 27, 2026

Copy link
Copy Markdown

Summary

writeCodePointToUTF16() in lib/Regex/RegexParser.cpp checked cp <= 0x10000 instead of isMemberOfBMP(cp) (cp <= 0xFFFF). Since U+10000 is a valid ID_Start character, an escaped U+10000 in a named capture group or \k<...> backreference (such as /(?<\u{10000}>a)/ or /(?<\uD800\uDC00>a)/) took the BMP branch and got cast to (char16_t)0x10000, storing \0 instead of the surrogate pair \uD800\uDC00.

This caused:

  • /(?<\u{10000}>a)/.exec("a").groups to expose key "\0" instead of "\u{10000}"
  • /(?<𐀀>a)\k<\u{10000}>/ to fail to compile with Nonexistent named capture reference
  • /(?<𐀀>a)(?<\u{10000}>b)/ to miss the duplicate capture group name check

Changed the check to isMemberOfBMP(cp) so U+10000 encodes as a surrogate pair, matching literal U+10000 group names.

Test Plan

  • Added Regex.NamedGroupU10000Escape in unittests/Support/RegexTest.cpp (verified it failed before the fix and passes after).
  • Added test/hermes/regress-regexp-group-name-u10000.js.

MinSizeRel build on macOS arm64:

cmake -B build -DCMAKE_BUILD_TYPE=MinSizeRel
cmake --build build -j8 --target HermesSupportTests hermes FileCheck
./build/unittests/Support/HermesSupportTests --gtest_filter="Regex.*"
./build/bin/hermes test/hermes/regress-regexp-group-name-u10000.js | ./build/bin/FileCheck --match-full-lines test/hermes/regress-regexp-group-name-u10000.js
./build/bin/hermes test/hermes/regexp-duplicate-named-groups.js | ./build/bin/FileCheck --match-full-lines test/hermes/regexp-duplicate-named-groups.js
./build/bin/hermes test/hermes/regexp.js | ./build/bin/FileCheck --match-full-lines test/hermes/regexp.js

Results:

  • HermesSupportTests --gtest_filter="Regex.*": 16 passed, 0 failed
  • regress-regexp-group-name-u10000.js, regexp-duplicate-named-groups.js, regexp.js: all passed with FileCheck

@meta-cla

meta-cla Bot commented Nov 27, 2026

Copy link
Copy Markdown

Hi @asxxb!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 27, 2026
@meta-cla

meta-cla Bot commented Nov 27, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant