Skip to content

Commit 1725ce0

Browse files
authored
Merge pull request libgit2#5224 from lrm29/check_if_repository_memory_leak
open:fix memory leak when passing NULL to git_repository_open_ext
2 parents c3a7892 + dde6d9c commit 1725ce0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/repository.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ int git_repository_open_ext(
800800
unsigned is_worktree;
801801
git_buf gitdir = GIT_BUF_INIT, workdir = GIT_BUF_INIT,
802802
gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
803-
git_repository *repo;
803+
git_repository *repo = NULL;
804804
git_config *config = NULL;
805805

806806
if (flags & GIT_REPOSITORY_OPEN_FROM_ENV)
@@ -813,7 +813,7 @@ int git_repository_open_ext(
813813
&gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
814814

815815
if (error < 0 || !repo_ptr)
816-
return error;
816+
goto cleanup;
817817

818818
repo = repository_alloc();
819819
GIT_ERROR_CHECK_ALLOC(repo);
@@ -859,11 +859,14 @@ int git_repository_open_ext(
859859
cleanup:
860860
git_buf_dispose(&gitdir);
861861
git_buf_dispose(&workdir);
862+
git_buf_dispose(&gitlink);
863+
git_buf_dispose(&commondir);
862864
git_config_free(config);
863865

864866
if (error < 0)
865867
git_repository_free(repo);
866-
else
868+
869+
if (repo_ptr)
867870
*repo_ptr = repo;
868871

869872
return error;

tests/repo/open.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ void test_repo_open__open_with_discover(void)
8888
cl_fixture_cleanup("attr");
8989
}
9090

91+
void test_repo_open__check_if_repository(void)
92+
{
93+
cl_git_sandbox_init("empty_standard_repo");
94+
95+
/* Pass NULL for the output parameter to check for but not open the repo */
96+
cl_git_pass(git_repository_open_ext(NULL, "empty_standard_repo", 0, NULL));
97+
cl_git_fail(git_repository_open_ext(NULL, "repo_does_not_exist", 0, NULL));
98+
99+
cl_fixture_cleanup("empty_standard_repo");
100+
}
101+
91102
static void make_gitlink_dir(const char *dir, const char *linktext)
92103
{
93104
git_buf path = GIT_BUF_INIT;

0 commit comments

Comments
 (0)