git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()

From
Lidong Yan via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 5, 2025, 06:27 UTC
Message-ID
<pull.1988.v2.git.git.1749104846531.gitgitgadget@gmail.com>
In-Reply-To
<pull.1988.git.git.1749006607791.gitgitgadget@gmail.com>
From: Lidong Yan <502024330056@smail.nju.edu.cn>

pretty.c:repo_logmsg_reencode() allocated memory should be freed with repo_unuse_commit_buffer(). Callers sometimes forgot free it at exit point. Add `repo_unuse_commit_buffer()` in insert_records_from_trailers at builtin/shortlog.c and create_commit at builtin/replay.c

Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
---
    repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()
    
    pretty.c:repo_logmsg_reencode() allocated memory should be freed with
    repo_unuse_commit_buffer(). Callers sometimes forgot free it at exit
    point. Add repo_unuse_commit_buffer() in insert_records_from_trailers at
    builtin/shortlog.c and create_commit at builtin/replay.c.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1988%2Fbrandb97%2Ffix-reencode-leak-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1988/brandb97/fix-reencode-leak-v2
Pull-Request: https://github.com/git/git/pull/1988
Range-diff vs v1:
 1:  a414074f167 ! 1:  f5165d6a102 repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()
     @@ builtin/replay.c: static struct commit *create_commit(struct repository *repo,
      
       ## builtin/shortlog.c ##
      @@ builtin/shortlog.c: 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;
     -+	}
     + 	if (!body)
     +-		return;
     ++		goto out;
       
       	trailer_iterator_init(&iter, body);
       	while (trailer_iterator_advance(&iter)) {
     +@@ builtin/shortlog.c: 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);
     + }
 builtin/replay.c   | 1 +
 builtin/shortlog.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
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);
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 30075b67be8..fe15e114973 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -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);
 }

base-commit: 7014b55638da979331baf8dc31c4e1d697cf2d67
-- 
gitgitgadget
Previous: lidongyanNext: Jeff King
Message 6 of 7 in “repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()”
  1. repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()Lidong Yan via GitGitGadget, Jun 4, 2025
  2. Patrick SteinhardtJun 4, 2025
  3. lidongyanJun 4, 2025
  4. Jeff KingJun 5, 2025
  5. lidongyanJun 6, 2025
  6. repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode()Lidong Yan via GitGitGadget, Jun 5, 2025
  7. Jeff KingJun 5, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.