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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 5, 2017 at 09:04 UTC
afbb8838b7d4d1887da1e1871f8e9ccd01ae1138
1 file changed
+13
-5
builtin/reset.c
+13
-5
@@ -44,10 +44,11 @@ static inline int is_merge(void)
44
45
static int reset_index(const struct object_id *oid, int reset_type, int quiet)
46
{
47
- int nr = 0;
47
+ int i, nr = 0;
48
struct tree_desc desc[2];
49
struct tree *tree;
50
struct unpack_trees_options opts;
51
+ int ret = -1;
52
53
memset(&opts, 0, sizeof(opts));
54
opts.head_idx = 1;
@@ -81,19 +82,26 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)
82
opts.fn = twoway_merge;
83
}
84
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
+ }
89
nr++;
90
91
if (unpack_trees(nr, desc, &opts))
89
- return -1;
92
+ goto out;
93
94
if (reset_type == MIXED || reset_type == HARD) {
95
tree = parse_tree_indirect(oid);
96
prime_cache_tree(&the_index, tree);
97
}
98
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;
105
}
106
107
static void print_new_head_line(struct commit *commit)