Merge pull request #1046 from libgit2/empty-remote-url-crash Bail out of remote loading if the URL would be NULL
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
diff --git a/src/remote.c b/src/remote.c
index 47bcaf9..a873a27 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -131,6 +131,12 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
if ((error = git_config_get_string(&val, config, git_buf_cstr(&buf))) < 0)
goto cleanup;
+
+ if (!val) {
+ giterr_set(GITERR_INVALID, "Malformed remote '%s' - missing URL", name);
+ error = -1;
+ goto cleanup;
+ }
remote->repo = repo;
remote->url = git__strdup(val);
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 21a3aaa..1d58aba 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -183,13 +183,13 @@ void test_network_remotes__list(void)
git_config *cfg;
cl_git_pass(git_remote_list(&list, _repo));
- cl_assert(list.count == 3);
+ cl_assert(list.count == 4);
git_strarray_free(&list);
cl_git_pass(git_repository_config(&cfg, _repo));
cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
cl_git_pass(git_remote_list(&list, _repo));
- cl_assert(list.count == 4);
+ cl_assert(list.count == 5);
git_strarray_free(&list);
git_config_free(cfg);
@@ -269,3 +269,11 @@ void test_network_remotes__tagopt(void)
git_config_free(cfg);
}
+
+void test_network_remotes__cannot_load_with_an_empty_url(void)
+{
+ git_remote *remote;
+
+ cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url"));
+ cl_assert(giterr_last()->klass == GITERR_INVALID);
+}
diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config
index 54ff610..3801ce0 100644
--- a/tests-clar/resources/testrepo.git/config
+++ b/tests-clar/resources/testrepo.git/config
@@ -8,6 +8,8 @@
fetch = +refs/heads/*:refs/remotes/test/*
[remote "joshaber"]
url = git://github.com/libgit2/libgit2
+[remote "empty-remote-url"]
+ url =
[remote "test_with_pushurl"]
url = git://github.com/libgit2/fetchlibgit2