Skip to content

Commit 48c924f

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

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/tree.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ 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'/'
396+
// parameter that includes a suffix, we have a prefix-suffix conflict.
397+
if common_prefix
398+
.checked_sub(1)
399+
.is_some_and(|i| common_remaining[i] != b'/')
398400
&& node.suffix_wild_child_in_segment()
399401
{
400402
return Err(InsertError::conflict(&route, remaining, node));

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)