Commit

6272f3bd174fcd1b394d8b5e05b2ba384bd9f63e

mktree: plug per-tree leak in --batch mode

In --batch mode "git mktree" reuses its entry buffer across trees,
resetting `used` to 0 after writing each tree.  It never frees the
`treeent` structures the previous tree appended, though, so once the
next tree overwrites those slots the earlier allocations are leaked.  A
single-tree invocation hides this, as the entries stay reachable through
the `entries` global until exit.

Free each entry when resetting the buffer, and free the buffer itself
before returning.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren <newren@gmail.com> · 2026-08-29 07:00 UTC · committed by Junio C Hamano

Browse files
1 file changed, +3 −0
modified builtin/mktree.c
@@ -200,8 +200,11 @@ int cmd_mktree(int ac,
200 200 puts(oid_to_hex(&oid));
201 201 fflush(stdout);
202 202 }
203 + for (int i = 0; i < used; i++)
204 + free(entries[i]);
203 205 used=0; /* reset tree entry buffer for re-use in batch mode */
204 206 }
207 + free(entries);
205 208 strbuf_release(&sb);
206 209
207 210 return 0;