Skip to content

Commit 5b8db44

Browse files
committed
format-patch: unleak "-v <num>"
The "subject_prefix" member of "struct revision" usually is set to a borrowed string (either a string literal like "PATCH" that appear in the program text as a hardcoded default, or the value of "format.subjectprefix") and is never freed when the containing revision structure is released. The "-v <num>" codepath however violates this rule and stores a pointer to an allocated string to this member, relinquishing the responsibility to free it when it is done using the revision structure, leading to a small one-time leak. Instead, keep track of the string it allocates to let the revision structure borrow, and clean it up when it is done. Signed-off-by: Junio C Hamano <[email protected]>
1 parent c48035d commit 5b8db44

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

builtin/log.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18701870
struct strbuf rdiff1 = STRBUF_INIT;
18711871
struct strbuf rdiff2 = STRBUF_INIT;
18721872
struct strbuf rdiff_title = STRBUF_INIT;
1873+
struct strbuf sprefix = STRBUF_INIT;
18731874
int creation_factor = -1;
18741875

18751876
const struct option builtin_format_patch_options[] = {
@@ -2010,12 +2011,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
20102011
cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
20112012

20122013
if (reroll_count) {
2013-
struct strbuf sprefix = STRBUF_INIT;
2014-
20152014
strbuf_addf(&sprefix, "%s v%s",
20162015
rev.subject_prefix, reroll_count);
20172016
rev.reroll_count = reroll_count;
2018-
rev.subject_prefix = strbuf_detach(&sprefix, NULL);
2017+
rev.subject_prefix = sprefix.buf;
20192018
}
20202019

20212020
for (i = 0; i < extra_hdr.nr; i++) {
@@ -2376,6 +2375,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
23762375
strbuf_release(&rdiff1);
23772376
strbuf_release(&rdiff2);
23782377
strbuf_release(&rdiff_title);
2378+
strbuf_release(&sprefix);
23792379
free(to_free);
23802380
if (rev.ref_message_ids)
23812381
string_list_clear(rev.ref_message_ids, 0);

0 commit comments

Comments
 (0)