Debian Codegraph @

debian/rust-vendor/regex-automata/tests/dfa/onepass/regression.rs

raw
1 // Regression test for zero-repetition capture groups,
2 // which caused a panic when the Vec passed into search_slots
3 // contained space for the capture group which would never
4 // have any results.
5 //
6 // See: https://github.com/rust-lang/regex/issues/1327
7 #[test]
8 fn zero_repetition_capture_group() {
9 use regex_automata::{
10 dfa::onepass::DFA, util::primitives::NonMaxUsize, Anchored, Input,
11 };
12
13 let expr = DFA::new(r"(abc)(ABC){0}").unwrap();
14 let s = "abcABC";
15 let input = Input::new(s).span(0..s.len()).anchored(Anchored::Yes);
16
17 // Test with 4 slots, so the whole match plus the first capture group.
18 let mut cache = expr.create_cache();
19 let mut slots: Vec<Option<NonMaxUsize>> = vec![None; 4];
20 let pid = expr.try_search_slots(&mut cache, &input, &mut slots).unwrap();
21 assert_eq!(pid, Some(regex_automata::PatternID::must(0)));
22 assert_eq!(slots[0], Some(NonMaxUsize::new(0).unwrap()));
23 assert_eq!(slots[1], Some(NonMaxUsize::new(3).unwrap()));
24 assert_eq!(slots[2], Some(NonMaxUsize::new(0).unwrap()));
25 assert_eq!(slots[3], Some(NonMaxUsize::new(3).unwrap()));
26
27 // Test with larger slot array, which would fit the
28 // zero-repetition capture group.
29 slots.resize(6, None);
30 let pid = expr.try_search_slots(&mut cache, &input, &mut slots).unwrap();
31 assert_eq!(pid, Some(regex_automata::PatternID::must(0)));
32 // First capture group should match
33 assert_eq!(slots[2], Some(NonMaxUsize::new(0).unwrap()));
34 assert_eq!(slots[3], Some(NonMaxUsize::new(3).unwrap()));
35 // Second capture group with {0} should be None.
36 assert_eq!(slots[4], None);
37 assert_eq!(slots[5], None);
38 }
39
40 // Another regression test for the same case as
41 // `zero_repetition_capture_group`, but uses a simpler pattern. That
42 // is, a zero-repetition capture group is a red herring. The actual bug
43 // is simpler: it happens whenever too many slots are provided by the
44 // caller.
45 #[test]
46 fn too_many_slots_normal_pattern() {
47 use regex_automata::{
48 dfa::onepass::DFA, util::primitives::NonMaxUsize, Anchored, Input,
49 };
50
51 let expr = DFA::new(r"abc").unwrap();
52 let s = "abc";
53 let input = Input::new(s).span(0..s.len()).anchored(Anchored::Yes);
54
55 let mut cache = expr.create_cache();
56 let mut slots: Vec<Option<NonMaxUsize>> = vec![None; 4];
57 let pid = expr.try_search_slots(&mut cache, &input, &mut slots).unwrap();
58 assert_eq!(pid, Some(regex_automata::PatternID::must(0)));
59 assert_eq!(slots[0], Some(NonMaxUsize::new(0).unwrap()));
60 assert_eq!(slots[1], Some(NonMaxUsize::new(3).unwrap()));
61 }