Skip to content

Commit 1154aa4

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc6b8fc commit 1154aa4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

builtin/fmt-merge-msg.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,14 @@ static void add_branch_desc(struct strbuf *out, const char *name)
227227

228228
#define util_as_integral(elem) ((intptr_t)((elem)->util))
229229

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)
232232
{
233-
const char *buffer;
234233
char *name_buf, *name, *name_end;
235234
struct string_list_item *elem;
236235
const char *field;
237236

238237
field = (which == 'a') ? "\nauthor " : "\ncommitter ";
239-
buffer = get_commit_buffer(commit);
240238
name = strstr(buffer, field);
241239
if (!name)
242240
return;
@@ -249,7 +247,6 @@ static void record_person(int which, struct string_list *people,
249247
if (name_end < name)
250248
return;
251249
name_buf = xmemdupz(name, name_end - name + 1);
252-
unuse_commit_buffer(commit, buffer);
253250

254251
elem = string_list_lookup(people, name_buf);
255252
if (!elem) {
@@ -260,6 +257,15 @@ static void record_person(int which, struct string_list *people,
260257
free(name_buf);
261258
}
262259

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+
263269
static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
264270
{
265271
const struct string_list_item *a = a_, *b = b_;

0 commit comments

Comments
 (0)