Skip to content

Commit 2efa106

Browse files
committed
Fix a lifetime bug uncovered by NLL, thanks @lqd
1 parent 7c27f90 commit 2efa106

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name = "url"
44
# When updating version, also modify html_root_url in the lib.rs
5-
version = "1.7.0"
5+
version = "1.7.1"
66
authors = ["The rust-url developers"]
77

88
description = "URL library for Rust, based on the WHATWG URL Standard"

src/form_urlencoded.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,16 @@ impl<'a> Target for &'a mut String {
257257
// * `Serializer` keeps its target in a private field
258258
// * Unlike in other `Target` impls, `UrlQuery::finished` does not return `Self`.
259259
impl<'a> Target for ::UrlQuery<'a> {
260-
fn as_mut_string(&mut self) -> &mut String { &mut self.url.serialization }
261-
fn finish(self) -> &'a mut ::Url { self.url }
260+
fn as_mut_string(&mut self) -> &mut String {
261+
&mut self.url.as_mut().unwrap().serialization
262+
}
263+
264+
fn finish(mut self) -> &'a mut ::Url {
265+
let url = self.url.take().unwrap();
266+
url.restore_already_parsed_fragment(self.fragment.take());
267+
url
268+
}
269+
262270
type Finished = &'a mut ::Url;
263271
}
264272

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ impl Url {
13431343
self.serialization.push('?');
13441344
}
13451345

1346-
let query = UrlQuery { url: self, fragment: fragment };
1346+
let query = UrlQuery { url: Some(self), fragment: fragment };
13471347
form_urlencoded::Serializer::for_suffix(query, query_start + "?".len())
13481348
}
13491349

@@ -2423,13 +2423,15 @@ fn io_error<T>(reason: &str) -> io::Result<T> {
24232423
/// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
24242424
#[derive(Debug)]
24252425
pub struct UrlQuery<'a> {
2426-
url: &'a mut Url,
2426+
url: Option<&'a mut Url>,
24272427
fragment: Option<String>,
24282428
}
24292429

24302430
impl<'a> Drop for UrlQuery<'a> {
24312431
fn drop(&mut self) {
2432-
self.url.restore_already_parsed_fragment(self.fragment.take())
2432+
if let Some(url) = self.url.take() {
2433+
url.restore_already_parsed_fragment(self.fragment.take())
2434+
}
24332435
}
24342436
}
24352437

0 commit comments

Comments
 (0)