From f82eb268379979e19f1af3d44f849d22062a31ab Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 2 Dec 2016 10:16:17 -0500 Subject: [PATCH] Prevent panic when calling `url::Url::set_host` with `None`. Fixes https://github.com/servo/rust-url/issues/243. --- src/lib.rs | 2 +- tests/unit.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 89e08c3c2..2fca96310 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1003,7 +1003,7 @@ impl Url { debug_assert!(self.byte_at(self.scheme_end) == b':'); debug_assert!(self.byte_at(self.path_start) == b'/'); let new_path_start = self.scheme_end + 1; - self.serialization.drain(self.path_start as usize..new_path_start as usize); + self.serialization.drain(new_path_start as usize..self.path_start as usize); let offset = self.path_start - new_path_start; self.path_start = new_path_start; self.username_end = new_path_start; diff --git a/tests/unit.rs b/tests/unit.rs index eb460015d..93ad1440c 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -317,3 +317,12 @@ fn append_empty_segment_then_mutate() { url.assert_invariants(); assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b"); } + +#[test] +fn test_set_host() { + let mut url = Url::parse("https://example.net/hello").unwrap(); + url.set_host(Some("foo.com")).unwrap(); + assert_eq!(url.as_str(), "https://foo.com/hello"); + url.set_host(None).unwrap(); + assert_eq!(url.as_str(), "https:/hello"); +}