Skip to content

Commit dab8527

Browse files
fix: prevent panic when parsing a URL with no path
Prior to this change `parse` would panic at `src/lib.rs:203:29` with `index out of bounds: the len is 0 but the index is 0` if given an input like "git:".
1 parent c88685b commit dab8527

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ impl GitUrl {
170170
normalized.scheme().to_string(),
171171
));
172172
};
173+
if normalized.path().is_empty() {
174+
return Err(GitUrlParseError::EmptyPath);
175+
}
173176

174177
// Normalized ssh urls can always have their first '/' removed
175178
let urlpath = match &scheme {
@@ -518,6 +521,8 @@ pub enum GitUrlParseError {
518521
UnsupportedUrlHostFormat,
519522
#[error("Git Url not in expected format for SSH")]
520523
UnsupportedSshUrlFormat,
524+
#[error("Normalized URL has no path")]
525+
EmptyPath,
521526

522527
#[error("Found null bytes within input url before parsing")]
523528
FoundNullBytes,

tests/parse.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ fn ssh_without_organization() {
367367
assert_eq!(parsed, expected);
368368
}
369369

370+
#[test]
371+
fn empty_path() {
372+
assert_eq!(
373+
GitUrlParseError::EmptyPath,
374+
GitUrl::parse("git:").unwrap_err()
375+
)
376+
}
377+
370378
#[test]
371379
fn bad_port_number() {
372380
let test_url = "https://github.com:crypto-browserify/browserify-rsa.git";

0 commit comments

Comments
 (0)