From 820bbe24984c77e0919943d2a2031b0ec7f15cbc Mon Sep 17 00:00:00 2001 From: InSync Date: Thu, 17 Oct 2024 06:40:44 +0000 Subject: [PATCH] Fix error leading to out-of-bound panic when a PEP 508 specifier starts with whitespace --- crates/uv-pep508/src/lib.rs | 2 +- crates/uv-pep508/src/tests.rs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/uv-pep508/src/lib.rs b/crates/uv-pep508/src/lib.rs index e2f432bbc89b..8668a9e5226c 100644 --- a/crates/uv-pep508/src/lib.rs +++ b/crates/uv-pep508/src/lib.rs @@ -933,7 +933,7 @@ fn parse_pep508_requirement( // // See: https://github.com/pypa/pip/blob/111eed14b6e9fba7c78a5ec2b7594812d17b5d2b/src/pip/_internal/utils/filetypes.py#L8 if requirement_kind.is_none() { - if looks_like_archive(cursor.slice(name_start, name_end)) { + if looks_like_archive(cursor.slice(name_start, name_end - name_start)) { let clone = cursor.clone().at(start); return Err(Pep508Error { message: Pep508ErrorSource::UnsupportedRequirement("URL requirement must be preceded by a package name. Add the name of the package before the URL (e.g., `package_name @ https://...`).".to_string()), diff --git a/crates/uv-pep508/src/tests.rs b/crates/uv-pep508/src/tests.rs index 661fedb18a56..970202ff4976 100644 --- a/crates/uv-pep508/src/tests.rs +++ b/crates/uv-pep508/src/tests.rs @@ -116,6 +116,12 @@ fn basic_examples() { assert_eq!(requests, expected); } +#[test] +fn leading_whitespace() { + let numpy = Requirement::::from_str(" numpy").unwrap(); + assert_eq!(numpy.name.as_ref(), "numpy"); +} + #[test] fn parenthesized_single() { let numpy = Requirement::::from_str("numpy ( >=1.19 )").unwrap();