fmt-merge-msg: plug small leak of commit buffer

A broken or badly formatted commit might not record author or committer lines or we may not find a valid name on them. The function record_person() returned after calling get_commit_buffer() without calling unuse_commit_buffer() on the memory it obtained in such cases, potentially leaking it. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 15, 2015 at 14:18 UTC 1154aa42159383c3339567f6385c21a615c8b93a
1 file changed +11 -5
builtin/fmt-merge-msg.c
+11 -5
@@ -227,16 +227,14 @@ static void add_branch_desc(struct strbuf *out, const char *name)
227
228 #define util_as_integral(elem) ((intptr_t)((elem)->util))
229
230 -static void record_person(int which, struct string_list *people,
231 - struct commit *commit)
230 +static void record_person_from_buf(int which, struct string_list *people,
231 + const char *buffer)
232 {
233 - const char *buffer;
233 char *name_buf, *name, *name_end;
234 struct string_list_item *elem;
235 const char *field;
236
237 field = (which == 'a') ? "\nauthor " : "\ncommitter ";
239 - buffer = get_commit_buffer(commit);
238 name = strstr(buffer, field);
239 if (!name)
240 return;
@@ -249,7 +247,6 @@ static void record_person(int which, struct string_list *people,
247 if (name_end < name)
248 return;
249 name_buf = xmemdupz(name, name_end - name + 1);
252 - unuse_commit_buffer(commit, buffer);
250
251 elem = string_list_lookup(people, name_buf);
252 if (!elem) {
@@ -260,6 +257,15 @@ static void record_person(int which, struct string_list *people,
257 free(name_buf);
258 }
259
260 +
261 +static void record_person(int which, struct string_list *people,
262 + struct commit *commit)
263 +{
264 + const char *buffer = get_commit_buffer(commit);
265 + record_person_from_buf(which, people, buffer);
266 + unuse_commit_buffer(commit, buffer);
267 +}
268 +
269 static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
270 {
271 const struct string_list_item *a = a_, *b = b_;