fix: validate named backreferences against existing groups#38
Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
Draft
fix: validate named backreferences against existing groups#38toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
Conversation
The parser accepted \k<foo>, \g{name}, (?P=name), (?&name), and
(?P>name) even when no matching named capture group existed anywhere
in the pattern. Perl itself rejects these with "Reference to
nonexistent named group".
Root cause: named_captures hash was only populated during the second
(tree-building) pass, and no validation checked it for backref/recursion
handlers. Numbered backrefs were already validated against maxpar.
Fix: populate named_captures during both passes (SIZE_ONLY uses maxpar,
tree-building uses nparen), then validate all named ref forms during the
second pass. Forward references work correctly because the first pass
has already recorded all group names.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Reject named backreferences and named recursion that reference nonexistent capture groups — matching Perl's own "Reference to nonexistent named group" error.
Why
The parser accepted
\k<foo>,\g{name},(?P=name),(?&name), and(?P>name)without checking whether a named group with that name exists anywhere in the pattern. Perl rejects all of these. Numbered backrefs (\1,\g{N}) were already validated againstmaxpar, but the named equivalents had no validation at all.How
named_capturesduring both parser passes (SIZE_ONLY and tree-building). Previously it was only populated during the second pass, so forward references to later-defined groups would fail validation.RPe_BGROUPerror checks in all 7 named ref/recursion handlers (\k<>,\k'',\k{},\g{name},(?P=),(?&),(?P>)), guarded by!&SIZE_ONLYto match the existing numbered backref pattern.(?&foo)(?<foo>a)work correctly because the first pass records all names.Testing
🤖 Generated with Claude Code
Quality Report
Changes: 2 files changed, 80 insertions(+), 9 deletions(-)
Code scan: clean
Tests: passed (OK)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline