summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-04-14 14:13:17 +0200
committernulltoken <emeric.fermas@gmail.com>2012-04-14 15:05:38 +0200
commitc1aefb35dd39efa0045a9925520b4715f82433e3 (patch)
treeb609115ab234252e16fff73a2b15ab84964e7d89
parentfdd1149c292727439c6616743ad044df3c74527c (diff)
downloadlibgit2-c1aefb35dd39efa0045a9925520b4715f82433e3.tar.gz
Fix git_repository_set_index() refcount issue
git_repository_free() calls git_index_free() if the owned index is not null. According to the doc, when setting a new index through git_repository_set_index() the caller has still to take care of releasing the index by itself. In order to cope with this, this fix makes sure the index refcount is incremented when a new repository is being plugged a new index.
-rw-r--r--src/repository.c1
-rw-r--r--tests-clar/repo/setters.c22
2 files changed, 23 insertions, 0 deletions
diff --git a/src/repository.c b/src/repository.c
index 413bb17ae..41a176a81 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -592,6 +592,7 @@ void git_repository_set_index(git_repository *repo, git_index *index)
repo->_index = index;
GIT_REFCOUNT_OWN(repo->_index, repo);
+ GIT_REFCOUNT_INC(index);
}
static int check_repositoryformatversion(git_repository *repo)
diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c
index 721eaaf2b..0c3b28d33 100644
--- a/tests-clar/repo/setters.c
+++ b/tests-clar/repo/setters.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "buffer.h"
#include "posix.h"
+#include "util.h"
static git_repository *repo;
@@ -35,3 +36,24 @@ void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
}
+
+void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void)
+{
+ git_index *new_index;
+
+ cl_git_pass(git_index_open(&new_index, "./my-index"));
+ cl_assert(((git_refcount *)new_index)->refcount == 1);
+
+ git_repository_set_index(repo, new_index);
+ cl_assert(((git_refcount *)new_index)->refcount == 2);
+
+ git_repository_free(repo);
+ cl_assert(((git_refcount *)new_index)->refcount == 1);
+
+ git_index_free(new_index);
+
+ /*
+ * Ensure the cleanup method won't try to free the repo as it's already been taken care of
+ */
+ repo = NULL;
+}