Commit 470a05d0d95ab8a840f1a851b1c498245ccfafaf

Sven Strickroth 2020-01-16T17:53:50

Do not return free'd git_repository object on error Regression introduced in commit dde6d9c706bf1ecab545da55ab874a016587af1f. This issue causes lots of crashes in TortoiseGit. Signed-off-by: Sven Strickroth <email@cs-ware.de>

diff --git a/src/repository.c b/src/repository.c
index 14968d7..03f6820 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -865,8 +865,7 @@ cleanup:
 
 	if (error < 0)
 		git_repository_free(repo);
-
-	if (repo_ptr)
+	else if (repo_ptr)
 		*repo_ptr = repo;
 
 	return error;
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 83b53c4..448ccdc 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -277,7 +277,9 @@ void test_repo_open__bad_gitlinks(void)
 
 	for (scan = bad_links; *scan != NULL; scan++) {
 		make_gitlink_dir("alternate", *scan);
+		repo = NULL;
 		cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL));
+		cl_assert(repo == NULL);
 	}
 
 	git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES);