Skip to content

Commit b545be3

Browse files
Laurence McGlashanLaurence McGlashan
authored andcommitted
open:fix memory leak when passing NULL to git_repository_open_ext
1 parent c3a7892 commit b545be3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/repository.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,13 @@ int git_repository_open_ext(
812812
error = find_repo(
813813
&gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
814814

815-
if (error < 0 || !repo_ptr)
815+
if (error < 0 || !repo_ptr) {
816+
git_buf_dispose(&gitdir);
817+
git_buf_dispose(&workdir);
818+
git_buf_dispose(&gitlink);
819+
git_buf_dispose(&commondir);
816820
return error;
821+
}
817822

818823
repo = repository_alloc();
819824
GIT_ERROR_CHECK_ALLOC(repo);

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)