Skip to content

Commit 89f45cf

Browse files
avargitster
authored andcommitted
format-patch: don't leak "extra_headers" or "ref_message_ids"
Fix two memory leaks in "struct rev_info" by freeing that memory in cmd_format_patch(). These two are unusual special-cases in being in the "struct rev_info", but not being "owned" by the code in revision.c. I.e. they're members of the struct so that this code in "builtin/log.c" can conveniently pass information code in "log-tree.c". See e.g. the make_cover_letter() caller of log_write_email_headers() here in "builtin/log.c", and [1] for a demonstration of where the "extra_headers" and "ref_message_ids" struct members are used. See 20ff068 (format-patch: resurrect extra headers from config, 2006-06-02) and d1566f7 (git-format-patch: Make the second and subsequent mails replies to the first, 2006-07-14) for the initial introduction of "extra_headers" and "ref_message_ids". We can count on repo_init_revisions() memset()-ing this data to 0 however, so we can count on it being either NULL or something we allocated. In the case of "extra_headers" let's add a local "char *" variable to hold it, to avoid the eventual cast from "const char *" when we free() it. 1. https://lore.kernel.org/git/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f260505 commit 89f45cf

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

builtin/log.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
17471747
struct commit *commit;
17481748
struct commit **list = NULL;
17491749
struct rev_info rev;
1750+
char *to_free = NULL;
17501751
struct setup_revision_opt s_r_opt;
17511752
int nr = 0, total, i;
17521753
int use_stdout = 0;
@@ -1947,7 +1948,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19471948
strbuf_addch(&buf, '\n');
19481949
}
19491950

1950-
rev.extra_headers = strbuf_detach(&buf, NULL);
1951+
rev.extra_headers = to_free = strbuf_detach(&buf, NULL);
19511952

19521953
if (from) {
19531954
if (split_ident_line(&rev.from_ident, from, strlen(from)))
@@ -2284,6 +2285,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
22842285
strbuf_release(&rdiff1);
22852286
strbuf_release(&rdiff2);
22862287
strbuf_release(&rdiff_title);
2288+
free(to_free);
2289+
if (rev.ref_message_ids)
2290+
string_list_clear(rev.ref_message_ids, 0);
2291+
free(rev.ref_message_ids);
22872292
UNLEAK(rev);
22882293
return 0;
22892294
}

0 commit comments

Comments
 (0)