Skip to content

Commit 1643e3e

Browse files
committed
Ensure DB backends haven't already been freed
When DB backends are successfully attached to the DB instacnes they get freed along with the DB so we don't have to. Also, the free method on the backends is optional so we shouldn't assume it is present. Fixes: #524
1 parent 7d00d84 commit 1643e3e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

ext/rugged/rugged_repo.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ static void load_alternates(git_repository *repo, VALUE rb_alternates)
189189
static void rugged_repo_new_with_backend(git_repository **repo, VALUE rb_path, VALUE rb_backend)
190190
{
191191
char *path;
192+
int odb_associated = 0;
193+
int refdb_associated = 0;
192194

193195
git_odb *odb = NULL;
194196
git_odb_backend *odb_backend = NULL;
@@ -215,7 +217,10 @@ static void rugged_repo_new_with_backend(git_repository **repo, VALUE rb_path, V
215217
if (error) goto cleanup;
216218

217219
error = git_odb_add_backend(odb, odb_backend, 1);
218-
if (error) goto cleanup;
220+
if (error)
221+
goto cleanup;
222+
else
223+
odb_associated = 1;
219224

220225
error = git_repository_wrap_odb(repo, odb);
221226
if (error) goto cleanup;
@@ -224,7 +229,10 @@ static void rugged_repo_new_with_backend(git_repository **repo, VALUE rb_path, V
224229
if (error) goto cleanup;
225230

226231
error = backend->refdb_backend(&refdb_backend, backend, path);
227-
if (error) goto cleanup;
232+
if (error)
233+
goto cleanup;
234+
else
235+
refdb_associated = 1;
228236

229237
error = git_refdb_set_backend(refdb, refdb_backend);
230238
if (error) goto cleanup;
@@ -248,8 +256,8 @@ static void rugged_repo_new_with_backend(git_repository **repo, VALUE rb_path, V
248256
git_odb_free(odb);
249257
git_refdb_free(refdb);
250258

251-
if (odb_backend != NULL) odb_backend->free(odb_backend);
252-
if (refdb_backend != NULL) refdb_backend->free(refdb_backend);
259+
if (odb_backend != NULL && !odb_associated && odb_backend->free) odb_backend->free(odb_backend);
260+
if (refdb_backend != NULL && !refdb_associated && refdb_backend->free) refdb_backend->free(refdb_backend);
253261

254262
rugged_exception_check(error);
255263
}

0 commit comments

Comments
 (0)