]> git.ipfire.org Git - thirdparty/git.git/commitdiff
format-patch: unleak "-v <num>"
authorJunio C Hamano <gitster@pobox.com>
Sun, 15 Jan 2023 08:03:39 +0000 (00:03 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 Jan 2023 18:31:45 +0000 (10:31 -0800)
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 <gitster@pobox.com>
builtin/log.c

index 89447a50838562795bb80172db8102ff7a42e91b..e3673e9dc1850fa91fe1cba20a0b1782b26840a1 100644 (file)
@@ -1870,6 +1870,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        struct strbuf rdiff1 = STRBUF_INIT;
        struct strbuf rdiff2 = STRBUF_INIT;
        struct strbuf rdiff_title = STRBUF_INIT;
+       struct strbuf sprefix = STRBUF_INIT;
        int creation_factor = -1;
 
        const struct option builtin_format_patch_options[] = {
@@ -2010,12 +2011,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
 
        if (reroll_count) {
-               struct strbuf sprefix = STRBUF_INIT;
-
                strbuf_addf(&sprefix, "%s v%s",
                            rev.subject_prefix, reroll_count);
                rev.reroll_count = reroll_count;
-               rev.subject_prefix = strbuf_detach(&sprefix, NULL);
+               rev.subject_prefix = sprefix.buf;
        }
 
        for (i = 0; i < extra_hdr.nr; i++) {
@@ -2376,6 +2375,7 @@ done:
        strbuf_release(&rdiff1);
        strbuf_release(&rdiff2);
        strbuf_release(&rdiff_title);
+       strbuf_release(&sprefix);
        free(to_free);
        if (rev.ref_message_ids)
                string_list_clear(rev.ref_message_ids, 0);