check for identical sequences in lcs to avoid overflow panic (#43)

This commit is contained in:
alexjg 2022-11-14 20:39:48 +00:00 committed by GitHub
parent 77c20faf94
commit 679c32ddee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -64,6 +64,12 @@ where
let common_prefix_len = common_prefix_len(old, old_range.clone(), new, new_range.clone());
let common_suffix_len = common_suffix_len(old, old_range.clone(), new, new_range.clone());
// If the sequences are not different then we're done
if common_prefix_len == old_range.len() && (old_range.len() == new_range.len()) {
d.equal(0, 0, old_range.len())?;
return Ok(());
}
let maybe_table = make_table(
old,
common_prefix_len..(old_range.len() - common_suffix_len),
@ -218,3 +224,13 @@ fn test_pat() {
diff(&mut d, a, 0..a.len(), b, 0..b.len()).unwrap();
insta::assert_debug_snapshot!(d.ops());
}
#[test]
fn test_same() {
let a: &[usize] = &[0, 1, 2, 3, 4, 4, 4, 5];
let b: &[usize] = &[0, 1, 2, 3, 4, 4, 4, 5];
let mut d = crate::algorithms::Capture::new();
diff(&mut d, a, 0..a.len(), b, 0..b.len()).unwrap();
insta::assert_debug_snapshot!(d.ops());
}

View file

@ -0,0 +1,12 @@
---
source: src/algorithms/lcs.rs
assertion_line: 235
expression: d.ops()
---
[
Equal {
old_index: 0,
new_index: 0,
len: 8,
},
]