Skip to content

Commit b1bcad1

Browse files
committed
avoid underflow when inserting route without leading slash
1 parent f607f73 commit b1bcad1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/tree.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ impl<T> Node<T> {
393393
}
394394

395395
// Similarly, if we are inserting a longer prefix, and there is a route that leads to this
396-
// parameter that includes a suffix, we have a prefix-suffix conflicts.
397-
if common_remaining[common_prefix - 1] != b'/'
398-
&& node.suffix_wild_child_in_segment()
399-
{
400-
return Err(InsertError::conflict(&route, remaining, node));
396+
// parameter that includes a suffix, we have a prefix-suffix conflict.
397+
if let Some(i) = common_prefix.checked_sub(1) {
398+
if common_remaining[i] != b'/' && node.suffix_wild_child_in_segment() {
399+
return Err(InsertError::conflict(&route, remaining, node));
400+
}
401401
}
402402
}
403403

tests/insert.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ fn conflict(with: &'static str) -> InsertError {
1616
InsertError::Conflict { with: with.into() }
1717
}
1818

19-
// https://github.com/ibraheemdev/matchit/issues/84
19+
// Regression test for https://github.com/ibraheemdev/matchit/issues/84.
2020
#[test]
21-
fn root_prefix_issue() {
22-
InsertTest(vec![("{foo}", Ok(())), ("{foo}suffix", Ok(()))]).run()
21+
fn missing_leading_slash_suffix() {
22+
InsertTest(vec![("/{foo}", Ok(())), ("/{foo}suffix", Ok(()))]).run();
23+
InsertTest(vec![("{foo}", Ok(())), ("{foo}suffix", Ok(()))]).run();
24+
}
25+
26+
// Regression test for https://github.com/ibraheemdev/matchit/issues/82.
27+
#[test]
28+
fn missing_leading_slash_conflict() {
29+
InsertTest(vec![("{foo}/", Ok(())), ("foo/", Ok(()))]).run();
30+
InsertTest(vec![("foo/", Ok(())), ("{foo}/", Ok(()))]).run();
2331
}
2432

2533
#[test]

0 commit comments

Comments
 (0)