make the URI parser tolerate trailing slashes at the end of the input URI ok naddy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/lib/dial.c b/lib/dial.c
index 6c0be0a..2e29508 100644
--- a/lib/dial.c
+++ b/lib/dial.c
@@ -83,7 +83,6 @@ got_dial_parse_uri(char **proto, char **host, char **port,
{
const struct got_error *err = NULL;
char *s, *p, *q;
- int n;
*proto = *host = *port = *server_path = *repo_name = NULL;
@@ -112,6 +111,10 @@ got_dial_parse_uri(char **proto, char **host, char **port,
err = got_error_from_errno("strndup");
goto done;
}
+ if ((*host)[0] == '\0') {
+ err = got_error(GOT_ERR_PARSE_URI);
+ goto done;
+ }
p = q + 1;
} else {
*proto = strndup(uri, p - uri);
@@ -134,17 +137,29 @@ got_dial_parse_uri(char **proto, char **host, char **port,
err = got_error_from_errno("strndup");
goto done;
}
+ if ((*host)[0] == '\0') {
+ err = got_error(GOT_ERR_PARSE_URI);
+ goto done;
+ }
*port = strndup(q + 1, p - (q + 1));
if (*port == NULL) {
err = got_error_from_errno("strndup");
goto done;
}
+ if ((*port)[0] == '\0') {
+ err = got_error(GOT_ERR_PARSE_URI);
+ goto done;
+ }
} else {
*host = strndup(s, p - s);
if (*host == NULL) {
err = got_error_from_errno("strndup");
goto done;
}
+ if ((*host)[0] == '\0') {
+ err = got_error(GOT_ERR_PARSE_URI);
+ goto done;
+ }
}
}
@@ -156,25 +171,18 @@ got_dial_parse_uri(char **proto, char **host, char **port,
goto done;
}
got_path_strip_trailing_slashes(*server_path);
-
- p = strrchr(p, '/');
- if (!p || strlen(p) <= 1) {
- err = got_error(GOT_ERR_PARSE_URI);
- goto done;
- }
- p++;
- n = strlen(p);
- if (n == 0) {
+ if ((*server_path)[0] == '\0') {
err = got_error(GOT_ERR_PARSE_URI);
goto done;
}
- if (hassuffix(p, ".git"))
- n -= 4;
- *repo_name = strndup(p, (p + n) - p);
- if (*repo_name == NULL) {
- err = got_error_from_errno("strndup");
+
+ err = got_path_basename(repo_name, *server_path);
+ if (err)
goto done;
- }
+ if (hassuffix(*repo_name, ".git"))
+ (*repo_name)[strlen(*repo_name) - 4] = '\0';
+ if ((*repo_name)[0] == '\0')
+ err = got_error(GOT_ERR_PARSE_URI);
done:
if (err) {
free(*proto);
diff --git a/regress/fetch/fetch_test.c b/regress/fetch/fetch_test.c
index 6645239..4afec39 100644
--- a/regress/fetch/fetch_test.c
+++ b/regress/fetch/fetch_test.c
@@ -78,7 +78,7 @@ fetch_parse_uri(void)
{ "git://localhost////",
NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
{ "git://127.0.0.1/git/",
- NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
+ "git", "127.0.0.1", NULL, "/git", "git", GOT_ERR_OK },
{ "git:///127.0.0.1/git/",
NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
{ "/127.0.0.1:/git/",