summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurence McGlashan <Laurence.McGlashan@mathworks.co.uk>2019-09-10 11:14:36 +0100
committerPatrick Steinhardt <ps@pks.im>2020-03-26 16:20:17 +0100
commit8c99ccc5d555dc04369bfd16dcbc4c4d5778a10f (patch)
treea16e38f0a195ecfd2746970573da65aa9ca7cf64
parent35168571973d25882837a1c841c689b79a012b23 (diff)
downloadlibgit2-8c99ccc5d555dc04369bfd16dcbc4c4d5778a10f.tar.gz
open:fix memory leak when passing NULL to git_repository_open_ext
-rw-r--r--src/repository.c7
-rw-r--r--tests/repo/open.c11
2 files changed, 17 insertions, 1 deletions
diff --git a/src/repository.c b/src/repository.c
index 26936a82f..cb6be60b4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -811,8 +811,13 @@ int git_repository_open_ext(
error = find_repo(
&gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
- if (error < 0 || !repo_ptr)
+ if (error < 0 || !repo_ptr) {
+ git_buf_dispose(&gitdir);
+ git_buf_dispose(&workdir);
+ git_buf_dispose(&gitlink);
+ git_buf_dispose(&commondir);
return error;
+ }
repo = repository_alloc();
GIT_ERROR_CHECK_ALLOC(repo);
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 06ec71bfd..9bf24354b 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -88,6 +88,17 @@ void test_repo_open__open_with_discover(void)
cl_fixture_cleanup("attr");
}
+void test_repo_open__check_if_repository(void)
+{
+ cl_git_sandbox_init("empty_standard_repo");
+
+ /* Pass NULL for the output parameter to check for but not open the repo */
+ cl_git_pass(git_repository_open_ext(NULL, "empty_standard_repo", 0, NULL));
+ cl_git_fail(git_repository_open_ext(NULL, "repo_does_not_exist", 0, NULL));
+
+ cl_fixture_cleanup("empty_standard_repo");
+}
+
static void make_gitlink_dir(const char *dir, const char *linktext)
{
git_buf path = GIT_BUF_INIT;