Skip to content

Commit afbb883

Browse files
peffgitster
authored andcommitted
reset: free allocated tree buffers
We read the tree objects with fill_tree_descriptor(), but never actually free the resulting buffers, causing a memory leak. This isn't a huge deal because we call this code at most twice per program invocation. But it does potentially double our heap usage if you have large root trees. Let's free the trees before returning. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9ce897 commit afbb883

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

builtin/reset.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ static inline int is_merge(void)
4444

4545
static int reset_index(const struct object_id *oid, int reset_type, int quiet)
4646
{
47-
int nr = 0;
47+
int i, nr = 0;
4848
struct tree_desc desc[2];
4949
struct tree *tree;
5050
struct unpack_trees_options opts;
51+
int ret = -1;
5152

5253
memset(&opts, 0, sizeof(opts));
5354
opts.head_idx = 1;
@@ -81,19 +82,26 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)
8182
opts.fn = twoway_merge;
8283
}
8384

84-
if (!fill_tree_descriptor(desc + nr, oid))
85-
return error(_("Failed to find tree of %s."), oid_to_hex(oid));
85+
if (!fill_tree_descriptor(desc + nr, oid)) {
86+
error(_("Failed to find tree of %s."), oid_to_hex(oid));
87+
goto out;
88+
}
8689
nr++;
8790

8891
if (unpack_trees(nr, desc, &opts))
89-
return -1;
92+
goto out;
9093

9194
if (reset_type == MIXED || reset_type == HARD) {
9295
tree = parse_tree_indirect(oid);
9396
prime_cache_tree(&the_index, tree);
9497
}
9598

96-
return 0;
99+
ret = 0;
100+
101+
out:
102+
for (i = 0; i < nr; i++)
103+
free((void *)desc[i].buffer);
104+
return ret;
97105
}
98106

99107
static void print_new_head_line(struct commit *commit)

0 commit comments

Comments
 (0)