Skip to content

Fix ^ inside lookbehind alternations and loops - #2247

Open
idhanth wants to merge 1 commit into
meta:static_hfrom
idhanth:fix-2233-lookbehind-anchored-alternative
Open

idhanth wants to merge 1 commit into
meta:static_hfrom
idhanth:fix-2233-lookbehind-anchored-alternative

Conversation

@idhanth

@idhanth idhanth commented Oct 15, 2026

Copy link
Copy Markdown

Summary

Fixes #2233.

/a(?<=x|^a)/.test("a") returns false instead of true.

Regex::pushLookaround reverses a lookbehind's node list after its nested nodes were constructed. AlternationNode and LoopNode cache their children's match constraints at construction, so a ^ inside them leaves MatchConstraintAnchoredAtStart set. That flag reaches the executor as the alternation's primaryConstraints/secondaryConstraints and the loop's loopeeConstraints. satisfiesConstraints then rejects the ^a alternative, because the backwards match starts at position 1 (after the a), not at the start of the input.

LookaroundNode already clears MatchConstraintAnchoredAtStart for the top level of a lookbehind. This change clears it in AlternationNode::reverseChildren and LoopNode::reverseChildren as well. The constraint is only an early-reject optimization: the ^ itself is still checked when its LeftAnchor instruction runs, so clearing it can't make a regex match where it shouldn't.

The same bug affected loops: /a(?<=(?:^a)+)/ didn't match "a" either.

Not changed:

  • MarkedSubexpressionNode caches constraints too, but only its compile-time matchConstraints() reads them, before reversal, so they never reach the executor.
  • A lookahead nested in a lookbehind isn't reversed (LookaroundNode doesn't override reverseChildren), so its constraints stay correct.

Test Plan

Added 8 cases to test/hermes/regexp.js. They cover the issue's pattern, a ^ in a later alternative, a capture group, unbounded and bounded loops, a loop around an alternation, and two cases where ^ must still reject. Every expected result matches V8 (Node 22).

Built on Ubuntu 24.04 (clang 18, Release, linux/arm64 in Docker) from static_h:

  • Without the fix, 6 of the 8 new cases print null where V8 returns a match. With it, all 8 match V8.
  • test/hermes/regexp.js and test/hermes/string-functions.js pass when run through their RUN lines with the built hermes and FileCheck.
  • LIT_FILTER="(?i)regex|regexp" ninja check-hermes: 36 of 4321 tests selected, 35 passed, 1 unsupported, 0 failed.
  • clang-format --dry-run --Werror include/hermes/Regex/RegexNode.h (clang-format 18, repo .clang-format): clean.

The full check-hermes suite was not run.

🤖 Generated with Claude Code

A lookbehind's nodes are reversed after nested AlternationNode and
LoopNode instances have cached MatchConstraintAnchoredAtStart. The
executor then rejected those alternatives and loopees, because a
backwards match starts after the anchored text, not at the start of the
input. For example, /a(?<=x|^a)/ failed to match "a".

Clear the stale constraint when these nodes reverse their children, as
LookaroundNode already does for the top level of a lookbehind.

Fixes meta#2233

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla

meta-cla Bot commented Oct 15, 2026

Copy link
Copy Markdown

Hi @idhanth!

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 cla@meta.com. 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regex lookbehind with anchored alternative fails to match

1 participant