From: Junio C Hamano <gitster@pobox.com>
To: "Kevin Backhouse via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Kevin Backhouse <kevinbackhouse@github.com>,
Elijah Newren <newren@gmail.com>
Subject: Re: [PATCH] This fixes a minor memory leak (detected by LeakSanitizer) in git merge.
Date: Fri, 18 Aug 2023 14:41:41 -0700 [thread overview]
Message-ID: <xmqqo7j4t4dm.fsf@gitster.g> (raw)
In-Reply-To: <pull.1577.git.1692389061490.gitgitgadget@gmail.com> (Kevin Backhouse via GitGitGadget's message of "Fri, 18 Aug 2023 20:04:21 +0000")
"Kevin Backhouse via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Kevin Backhouse <kevinbackhouse@github.com>
>
> To reproduce (with an ASAN build):
>
> ```
> mkdir test
> cd test
> git init
> echo x > x.txt
> git add .
> git commit -m "WIP"
> git checkout -b dev
> echo y > x.txt
> git add .
> git commit -m "WIP"
> git checkout main
> echo z > x.txt
> git add .
> git commit -m "WIP"
> echo a > x.txt
> git add .
> git merge dev
> ```
We'd rather not to see the above in the proposed log message; can't
we add (a variation of) it to our test suite?
> The fix is to call free_commit_list(merge_bases) when an error occurs.
We usually have the description of what the problem is and give an
analysis on why/how it happens, before presenting a solution. Write
it more like:
The caller of merge_ort_recursive() expects the commit list
passed in as the merge_bases parameter to be fully consumed by
the function and does not free it when the function returns. In
normal cases, the commit list does get consumed, but when the
function returns early upon encountering an error, it forgets to
clean it up.
Fix this by freeing the list in the code paths for error returns.
> merge-ort-wrappers.c | 4 +++-
> merge-ort.c | 4 +++-
These two places and their fixes seem OK, but I have to wonder if
these are complete fixes.
> diff --git a/merge-ort-wrappers.c b/merge-ort-wrappers.c
> index 4acedf3c338..aeb56c9970c 100644
> --- a/merge-ort-wrappers.c
> +++ b/merge-ort-wrappers.c
> @@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
> struct tree *head = repo_get_commit_tree(opt->repo, side1);
> struct merge_result tmp;
>
> - if (unclean(opt, head))
> + if (unclean(opt, head)) {
> + free_commit_list(merge_bases);
> return -1;
> + }
>
> memset(&tmp, 0, sizeof(tmp));
> merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);
The function before this hunk appears to have very similar code
structure. Does it need the same fix, or if not why not?
> diff --git a/merge-ort.c b/merge-ort.c
> index 8631c997002..a0eb91fb011 100644
> --- a/merge-ort.c
> +++ b/merge-ort.c
> @@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
> opt->branch1 = "Temporary merge branch 1";
> opt->branch2 = "Temporary merge branch 2";
> merge_ort_internal(opt, NULL, prev, next, result);
> - if (result->clean < 0)
> + if (result->clean < 0) {
> + free_commit_list(merge_bases);
> return;
> + }
Before this function, there is a comment that this came from another
function and it seems to still have a very similar code structure.
Does the other function need the same fix, or if not why not?
Thanks.
next prev parent reply other threads:[~2023-08-18 21:42 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-18 20:04 [PATCH] This fixes a minor memory leak (detected by LeakSanitizer) in git merge Kevin Backhouse via GitGitGadget
2023-08-18 21:41 ` Junio C Hamano [this message]
2023-09-12 15:06 ` Elijah Newren
2023-08-24 14:12 ` [PATCH v2 0/2] " Kevin Backhouse via GitGitGadget
2023-08-24 14:12 ` [PATCH v2 1/2] Regression test for https://github.com/gitgitgadget/git/pull/1577 Kevin Backhouse via GitGitGadget
2023-08-24 15:11 ` Junio C Hamano
2023-08-24 14:12 ` [PATCH v2 2/2] Fix minor memory leak found by LeakSanitizer Kevin Backhouse via GitGitGadget
2023-08-24 15:56 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqo7j4t4dm.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=kevinbackhouse@github.com \
--cc=newren@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).