fix panic OpMinUntil return before restore repeat

This commit is contained in:
Kangzhi Shi
2022-07-11 21:30:48 +02:00
parent c5871f4c2a
commit 74ebdaf4e8
3 changed files with 38 additions and 4 deletions

21
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,21 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug Unit Test",
"cargo": {
"args": [
"test",
"--no-run"
],
"filter": {
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View File

@@ -14,7 +14,7 @@ pub struct State<'a> {
pub string: StrDrive<'a>,
pub start: usize,
pub end: usize,
flags: SreFlag,
_flags: SreFlag,
pattern_codes: &'a [u32],
pub marks: Vec<Option<usize>>,
pub lastindex: isize,
@@ -42,7 +42,7 @@ impl<'a> State<'a> {
string,
start,
end,
flags,
_flags: flags,
pattern_codes,
lastindex: -1,
marks_stack: Vec::new(),
@@ -1380,16 +1380,18 @@ impl OpcodeExecutor for OpMinUntil {
None
}
2 => {
// restore repeat before return
drive.state.repeat_stack.push(self.save_repeat.unwrap());
let child_ctx = drive.state.popped_context.unwrap();
if child_ctx.has_matched == Some(true) {
drive.ctx_mut().has_matched = Some(true);
return None;
}
drive.state.repeat_stack.push(self.save_repeat.unwrap());
drive.state.string_position = drive.ctx().string_position;
drive.state.marks_pop();
// match more unital tail matches
// match more until tail matches
let RepeatContext {
count: _,
code_position,

View File

@@ -60,3 +60,14 @@ fn test_zerowidth() {
state = state.search();
assert!(state.string_position == 1);
}
#[test]
fn test_repeat_context_panic() {
// pattern p = re.compile(r'(?:a*?(xx)??z)*')
// START GENERATED by generate_tests.py
#[rustfmt::skip] let p = Pattern { code: &[15, 4, 0, 0, 4294967295, 24, 25, 0, 4294967295, 27, 6, 0, 4294967295, 17, 97, 1, 24, 11, 0, 1, 18, 0, 17, 120, 17, 120, 18, 1, 20, 17, 122, 19, 1], flags: SreFlag::from_bits_truncate(32) };
// END GENERATED
let mut state = p.state("axxzaz", 0..usize::MAX);
state = state.pymatch();
assert!(state.marks == vec![Some(1), Some(3)]);
}