Skip to content

Commit

Permalink
url: fix panic on popping from Url without path (fixes #656)
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 8, 2020
1 parent 6865d52 commit 3174ea5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions url/src/path_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ impl<'a> PathSegmentsMut<'a> {
/// # run().unwrap();
/// ```
pub fn pop_if_empty(&mut self) -> &mut Self {
if self.after_first_slash >= self.url.serialization.len() {
return self;
}
if self.url.serialization[self.after_first_slash..].ends_with('/') {
self.url.serialization.pop();
}
Expand All @@ -135,6 +138,9 @@ impl<'a> PathSegmentsMut<'a> {
///
/// Returns `&mut Self` so that method calls can be chained.
pub fn pop(&mut self) -> &mut Self {
if self.after_first_slash >= self.url.serialization.len() {
return self;
}
let last_slash = self.url.serialization[self.after_first_slash..]
.rfind('/')
.unwrap_or(0);
Expand Down
8 changes: 8 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,11 @@ fn no_panic() {
let mut url = Url::parse("arhttpsps:/.//eom/dae.com/\\\\t\\:").unwrap();
url::quirks::set_hostname(&mut url, "//eom/datcom/\\\\t\\://eom/data.cs").unwrap();
}

#[test]
fn pop_if_empty_in_bounds() {
let mut url = Url::parse("m://").unwrap();
let mut segments = url.path_segments_mut().unwrap();
segments.pop_if_empty();
segments.pop();
}

0 comments on commit 3174ea5

Please # to comment.