Skip to content

Fix onepass DFA panic with zero-repetition capture groups#1

Open
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-search-slots-panics
Open

Fix onepass DFA panic with zero-repetition capture groups#1
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-search-slots-panics

Conversation

Copy link
Copy Markdown

Copilot AI commented Jan 27, 2026

search_slots panicked when using regexes with zero-repetition capture groups (e.g., (abc){0}) and switching between regexes with different capture group counts.

let expr = regex_automata::meta::Regex::new(r"(abc)(ABC){0}").unwrap();
let expr2 = regex_automata::meta::Regex::new(r"(abc)(ABC)").unwrap();
let mut slots: Vec<Option<NonMaxUsize>> = vec![None; 6];

expr.search_slots(&input, &mut slots);   // Works
expr2.search_slots(&input, &mut slots);  // Works
expr.search_slots(&input, &mut slots);   // Panicked: range end index 4 out of range for slice of length 2

Changes

  • Cache::setup_search: Clamp explicit_slot_len to actual explicit_slots vector size to prevent out-of-bounds access
  • Slot copying logic: Copy only the minimum available slots from cache instead of assuming size match between cache and caller's array
  • Test coverage: Added regression test for zero-repetition capture groups with varying slot array sizes

Zero-repetition capture groups now correctly populate slots with None values as expected.

Original prompt

This code panics when the documentation states that search_slots should accept any size Vec. Let's make the necessary fixes to the code so it doesn't panic and the documentation becomes true. Zero repetition on a capture group means the capture group should be None...

use regex_automata::util::primitives::NonMaxUsize; use regex_automata::Anchored; use regex_automata::Input; fn main() { let expr = regex_automata::meta::Regex::new(r"(abc)(ABC){0}").unwrap(); let s = "abcABC"; dbg!(expr.is_match(s)); let mut inner_slots: Vec<Option<NonMaxUsize>> = Vec::new(); let input = Input::new(s).span(0..s.len()).anchored(Anchored::Yes); inner_slots.resize(4, None); dbg!(expr.search_slots(&input, &mut inner_slots)); dbg!(&inner_slots); inner_slots.resize(6, None); let expr2 = regex_automata::meta::Regex::new(r"(abc)(ABC)").unwrap(); dbg!(expr2.search_slots(&input, &mut inner_slots)); dbg!(&inner_slots); dbg!(expr.search_slots(&input, &mut inner_slots)); dbg!(&inner_slots); }

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix panic issue in search_slots for any size Vec Fix onepass DFA panic with zero-repetition capture groups Jan 27, 2026
Copilot AI requested a review from keith-hall January 27, 2026 02:37
@keith-hall keith-hall marked this pull request as ready for review January 27, 2026 03:11
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.

2 participants