public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks•im>
To: Lidong Yan via GitGitGadget <gitgitgadget@gmail•com>
Cc: git@vger•kernel.org, Lidong Yan <502024330056@smail•nju.edu.cn>
Subject: Re: [PATCH] repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()
Date: Wed, 4 Jun 2025 09:56:39 +0200	[thread overview]
Message-ID: <aD_8NxMi6Dk7CmSl@pks.im> (raw)
In-Reply-To: <pull.1988.git.git.1749006607791.gitgitgadget@gmail.com>

On Wed, Jun 04, 2025 at 03:10:07AM +0000, Lidong Yan via GitGitGadget wrote:
> diff --git a/builtin/replay.c b/builtin/replay.c
> index 225cef08807..6172c8aacc9 100644
> --- a/builtin/replay.c
> +++ b/builtin/replay.c
> @@ -84,6 +84,7 @@ static struct commit *create_commit(struct repository *repo,
>  	obj = parse_object(repo, &ret);
>  
>  out:
> +	repo_unuse_commit_buffer(the_repository, based_on, message);
>  	free_commit_extra_headers(extra);
>  	free_commit_list(parents);
>  	strbuf_release(&msg);

Makes sense. This one _looks_ like a leak that I'd expect to hit in our
test suite as it's not part of an error path.

> diff --git a/builtin/shortlog.c b/builtin/shortlog.c
> index 30075b67be8..dfc7e85ae96 100644
> --- a/builtin/shortlog.c
> +++ b/builtin/shortlog.c
> @@ -186,8 +186,10 @@ static void insert_records_from_trailers(struct shortlog *log,
>  	commit_buffer = repo_logmsg_reencode(the_repository, commit, NULL,
>  					     ctx->output_encoding);
>  	body = strstr(commit_buffer, "\n\n");
> -	if (!body)
> +	if (!body) {
> +		repo_unuse_commit_buffer(the_repository, commit, commit_buffer);
>  		return;
> +	}
>  
>  	trailer_iterator_init(&iter, body);
>  	while (trailer_iterator_advance(&iter)) {

Should this one maybe be converted into a `goto out` so that we can
release resources in a single location, only? Something like the below
patch.

Patrick

diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 30075b67be8..dd08bc40161 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -177,7 +177,7 @@ static void insert_records_from_trailers(struct shortlog *log,
 	struct strbuf ident = STRBUF_INIT;
 
 	if (!log->trailers.nr)
-		return;
+		goto out;
 
 	/*
 	 * Using repo_format_commit_message("%B") would be simpler here, but
@@ -187,7 +187,7 @@ static void insert_records_from_trailers(struct shortlog *log,
 					     ctx->output_encoding);
 	body = strstr(commit_buffer, "\n\n");
 	if (!body)
-		return;
+		goto out;
 
 	trailer_iterator_init(&iter, body);
 	while (trailer_iterator_advance(&iter)) {
@@ -206,6 +206,7 @@ static void insert_records_from_trailers(struct shortlog *log,
 	}
 	trailer_iterator_release(&iter);
 
+out:
 	strbuf_release(&ident);
 	repo_unuse_commit_buffer(the_repository, commit, commit_buffer);
 }

  reply	other threads:[~2025-06-04  7:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-04  3:10 [PATCH] repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode() Lidong Yan via GitGitGadget
2025-06-04  7:56 ` Patrick Steinhardt [this message]
2025-06-04 10:50   ` lidongyan
2025-06-05  7:23   ` Jeff King
2025-06-06 12:22     ` lidongyan
2025-06-05  6:27 ` [PATCH v2] " Lidong Yan via GitGitGadget
2025-06-05  7:53   ` Jeff King

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=aD_8NxMi6Dk7CmSl@pks.im \
    --to=ps@pks$(echo .)im \
    --cc=502024330056@smail$(echo .)nju.edu.cn \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitgitgadget@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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