Avoid panics for cannot-be-a-base URLs (#13406)

Following #13376, avoid `.unwrap()` on `Url::path_segments()`.

I also added some unwrap-safety comments.
This commit is contained in:
konsti
2025-05-13 04:29:26 +02:00
committed by GitHub
parent 6df588bb00
commit 3b125dbe71
2 changed files with 7 additions and 2 deletions
+3
View File
@@ -65,6 +65,8 @@ impl CanonicalUrl {
.is_some_and(|ext| ext.eq_ignore_ascii_case("git"));
if needs_chopping {
let last = {
// Unwrap safety: We checked `url.cannot_be_a_base()`, and `url.path()` having
// an extension implies at least one segment.
let last = url.path_segments().unwrap().next_back().unwrap();
last[..last.len() - 4].to_owned()
};
@@ -74,6 +76,7 @@ impl CanonicalUrl {
// Decode any percent-encoded characters in the path.
if memchr::memchr(b'%', url.path().as_bytes()).is_some() {
// Unwrap safety: We checked `url.cannot_be_a_base()`.
let decoded = url
.path_segments()
.unwrap()
+4 -2
View File
@@ -66,6 +66,8 @@ pub enum Error {
},
#[error("Invalid download URL")]
InvalidUrl(#[from] url::ParseError),
#[error("Invalid download URL: {0}")]
InvalidUrlFormat(Url),
#[error("Invalid path in file URL: `{0}`")]
InvalidFileUrl(String),
#[error("Failed to create download directory")]
@@ -643,9 +645,9 @@ impl ManagedPythonDownload {
// decodes to.
let filename = url
.path_segments()
.unwrap()
.ok_or_else(|| Error::InvalidUrlFormat(url.clone()))?
.next_back()
.unwrap()
.ok_or_else(|| Error::InvalidUrlFormat(url.clone()))?
.replace("%2B", "-");
debug_assert!(
filename