Skip to content

Commit 3174ea5

Browse files
committed
url: fix panic on popping from Url without path (fixes #656)
1 parent 6865d52 commit 3174ea5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

url/src/path_segments.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ impl<'a> PathSegmentsMut<'a> {
123123
/// # run().unwrap();
124124
/// ```
125125
pub fn pop_if_empty(&mut self) -> &mut Self {
126+
if self.after_first_slash >= self.url.serialization.len() {
127+
return self;
128+
}
126129
if self.url.serialization[self.after_first_slash..].ends_with('/') {
127130
self.url.serialization.pop();
128131
}
@@ -135,6 +138,9 @@ impl<'a> PathSegmentsMut<'a> {
135138
///
136139
/// Returns `&mut Self` so that method calls can be chained.
137140
pub fn pop(&mut self) -> &mut Self {
141+
if self.after_first_slash >= self.url.serialization.len() {
142+
return self;
143+
}
138144
let last_slash = self.url.serialization[self.after_first_slash..]
139145
.rfind('/')
140146
.unwrap_or(0);

url/tests/unit.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,11 @@ fn no_panic() {
671671
let mut url = Url::parse("arhttpsps:/.//eom/dae.com/\\\\t\\:").unwrap();
672672
url::quirks::set_hostname(&mut url, "//eom/datcom/\\\\t\\://eom/data.cs").unwrap();
673673
}
674+
675+
#[test]
676+
fn pop_if_empty_in_bounds() {
677+
let mut url = Url::parse("m://").unwrap();
678+
let mut segments = url.path_segments_mut().unwrap();
679+
segments.pop_if_empty();
680+
segments.pop();
681+
}

0 commit comments

Comments
 (0)