Skip to content

Fix auto-possessification across entered atomic groups - #1123

Open
saudademjj wants to merge 1 commit into
PCRE2Project:mainfrom
saudademjj:fix/auto-possess-entered-group-1100
Open

saudademjj wants to merge 1 commit into
PCRE2Project:mainfrom
saudademjj:fix/auto-possess-entered-group-1100

Conversation

@saudademjj

Copy link
Copy Markdown

In compare_opcodes(), entered_a_group was a local boolean that was not propagated into recursive calls for OP_ALT branches or OP_BRAZERO / OP_BRAMINZERO skip paths, and was only set after checking earlier alternation branches. When scanning forward from an iterator into an OP_ONCE group that can match empty (such as x?(?:y)?+x or x?(?>|y)x), the recursive call reached the closing OP_KET of OP_ONCE with entered_a_group == FALSE and returned TRUE without checking what followed the group.

This replaces the local boolean with a group_depth parameter tracking the number of open groups entered while scanning forward:

  • Increment group_depth before recursing on OP_ALT branches in OP_ONCE / OP_BRA / OP_CBRA.
  • Propagate group_depth through OP_BRAZERO / OP_BRAMINZERO skip calls.
  • Decrement group_depth when skipping past an entered group's closing ket in OP_KET / OP_KETRPOS.
  • At OP_ONCE, only return TRUE immediately when group_depth == 0 (the base iterator is inside the atomic group); when group_depth > 0, break out and continue inspecting what follows the group (which also preserves auto-possessification when the following item is disjoint, such as a+(?:bb|cc)?+#).

Added regression tests in testdata/testinput2 and testdata/testoutput2 covering optional possessive groups, empty/optional branches in atomic groups, nested groups, class repeats, and disjoint trailing items.

Verified with 8-, 16-, and 32-bit Debug + JIT builds (RunTest 2 and full ctest), as well as an ASan/UBSan build with -Werror (ctest 4/4 passed).

Fixes #1100.

In compare_opcodes(), entered_a_group was a local boolean that was not
propagated across recursive calls for OP_ALT branches or OP_BRAZERO /
OP_BRAMINZERO skip paths, and was only set after checking earlier
alternation branches. When scanning forward from an iterator into an
atomic group (OP_ONCE) that can match empty (such as x?(?:y)?+x or
x?(?>|y)x), a recursive call reaching the closing OP_KET of OP_ONCE saw
entered_a_group == FALSE and returned TRUE without inspecting what
followed the group.

Replace the local boolean with a group_depth parameter counting open
groups entered while scanning forward. Increment it before recursing on
OP_ALT branches in OP_ONCE / OP_BRA / OP_CBRA, propagate it through
OP_BRAZERO / OP_BRAMINZERO skip calls, and decrement it when exiting an
entered group at OP_KET / OP_KETRPOS. At OP_ONCE, only return TRUE
immediately when group_depth == 0 (the base iterator is inside the
atomic group); otherwise break out and continue inspecting what follows
the group.

Fixes PCRE2Project#1100.
@NWilson NWilson added this to the 10.51 (Jan/Feb 2027) milestone Dec 5, 2026

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.

Auto-possessification makes x? possessive before an optional possessive group, so x?(?:y)?+x fails to match "x"

2 participants