Skip to content

Fix backreference crash when optional group doesn't match#10933

Merged
thewilsonator merged 1 commit intodlang:masterfrom
emmanuel-ferdman:master
Jan 16, 2026
Merged

Fix backreference crash when optional group doesn't match#10933
thewilsonator merged 1 commit intodlang:masterfrom
emmanuel-ferdman:master

Conversation

@emmanuel-ferdman
Copy link
Contributor

@emmanuel-ferdman emmanuel-ferdman commented Jan 14, 2026

PR Summary

Backreferences to optional capture groups that didn't participate in a match would crash with an ArraySliceError. For example, regex(r"(a)?\1") matching against "xaa" would fail because the engine tried to slice the input using uninitialized group indices. This adds a check to verify the referenced group actually matched before attempting to use it. If the group is unmatched, the backreference now correctly fails to match rather than crashing. The fix covers all matcher implementations: runtime backtracker, Thompson NFA, and compile-time regex.

Closes #10905

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @emmanuel-ferdman! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10933"

@thewilsonator thewilsonator merged commit 690c74a into dlang:master Jan 16, 2026
9 of 10 checks passed
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.

std.regex: backreference to optional capture group throws Error when matching

3 participants