Skip to content

Commit 0ec0130

Browse files
committed
Fix memory leaks on error paths in listall_branches and diff_to_index
Fix Repository.listall_branches() leaking the result list when git_branch_iterator_new() fails. Fix Tree.diff_to_index() leaking the _pointer object when lazy-loading the tree fails. Fixes #1480 Assisted-by: Kimi Code
1 parent 794b7e1 commit 0ec0130

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
on allocation or per-item failure
2222
[#1479](https://github.com/libgit2/pygit2/issues/1479).
2323

24+
- Fix memory leaks in `Repository.listall_branches()` and
25+
`Tree.diff_to_index()` on early error paths
26+
[#1480](https://github.com/libgit2/pygit2/issues/1480).
27+
2428

2529
# 1.20.0 (2026-08-08)
2630

‎src/repository.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,10 @@ Repository_listall_branches_impl(Repository *self, PyObject *args, PyObject *(*i
14381438
if (list == NULL)
14391439
return NULL;
14401440

1441-
if ((err = git_branch_iterator_new(&iter, self->repo, list_flags)) < 0)
1441+
if ((err = git_branch_iterator_new(&iter, self->repo, list_flags)) < 0) {
1442+
Py_DECREF(list);
14421443
return Error_set(err);
1444+
}
14431445

14441446
while ((err = git_branch_next(&ref, &type, iter)) == 0) {
14451447
PyObject *py_branch_name = item_trans(git_reference_shorthand(ref));

‎src/tree.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Tree_diff_to_index(Tree *self, PyObject *args, PyObject *kwds)
308308
index = *((git_index **) buffer); /* the "buffer" contains the pointer */
309309

310310
/* Call git_diff_tree_to_index */
311-
if (Object__load((Object*)self) == NULL) { return NULL; } // Lazy load
311+
if (Object__load((Object*)self) == NULL) { goto error; } // Lazy load
312312

313313
err = git_diff_tree_to_index(&diff, self->repo->repo, self->tree, index, &opts);
314314
Py_DECREF(py_idx_ptr);

0 commit comments

Comments
 (0)