Git development
 help / color / mirror / Atom feed
From: "Onur Gündüz via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Onur Gündüz" <ogunduz@gmail.com>, "Onur Gündüz" <ogunduz@gmail.com>
Subject: [PATCH] blame: free the suspects slab at scoreboard teardown
Date: Thu, 26 Nov 2026 03:37:10 +0000	[thread overview]
Message-ID: <pull.2330.git.1795664230820.gitgitgadget@gmail.com> (raw)

From: =?UTF-8?q?Onur=20G=C3=BCnd=C3=BCz?= <ogunduz@gmail.com>

setup_scoreboard() initializes the file-static suspects slab each
time it runs, but cleanup_scoreboard() doesn't free the slab. A
second setup overwrites the pointer to the previous slab and leaks
its allocations.

Clear the slab during cleanup. Each final blame entry holds a
reference to its origin, so release those references first:
blame_origin_decref() uses the slab to unlink an origin when its
reference count reaches zero.

Add a regression test that sets up and cleans up two scoreboards
in one process. Without the fix, LeakSanitizer reports a leaked
slab; with the fix the test passes for both files and reftable.

Signed-off-by: Onur Gündüz <ogunduz@gmail.com>
---
    blame: free the suspects slab at scoreboard teardown

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2330%2Ftetsuo%2Fsubmit%2Fblame-lifetime-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2330/tetsuo/submit/blame-lifetime-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2330

 blame.c                     |  1 +
 blame.h                     |  1 +
 builtin/blame.c             |  1 +
 t/helper/test-repository.c  | 34 +++++++++++++++++++++++++++++++++-
 t/meson.build               |  1 +
 t/t8016-blame-scoreboard.sh | 12 ++++++++++++
 6 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100755 t/t8016-blame-scoreboard.sh

diff --git a/blame.c b/blame.c
index 126e232416..61e1a45650 100644
--- a/blame.c
+++ b/blame.c
@@ -2935,6 +2935,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)
 
 void cleanup_scoreboard(struct blame_scoreboard *sb)
 {
+	clear_blame_suspects(&blame_suspects);
 	free(sb->lineno);
 	free(sb->final_buf);
 	clear_prio_queue(&sb->commits);
diff --git a/blame.h b/blame.h
index 3b34be0e5c..75a910bb67 100644
--- a/blame.h
+++ b/blame.h
@@ -180,6 +180,7 @@ void init_scoreboard(struct blame_scoreboard *sb);
 void setup_scoreboard(struct blame_scoreboard *sb,
 		      struct blame_origin **orig);
 void setup_blame_bloom_data(struct blame_scoreboard *sb);
+/* Release all entry and caller references to origins before clearing the slab. */
 void cleanup_scoreboard(struct blame_scoreboard *sb);
 
 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
diff --git a/builtin/blame.c b/builtin/blame.c
index 48d5251c6d..332795d18f 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1323,6 +1323,7 @@ parse_done:
 cleanup:
 	for (ent = sb.ent; ent; ) {
 		struct blame_entry *e = ent->next;
+		blame_origin_decref(ent->suspect);
 		free(ent);
 		ent = e;
 	}
diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c
index 9ba94cdffa..245253efbd 100644
--- a/t/helper/test-repository.c
+++ b/t/helper/test-repository.c
@@ -1,12 +1,14 @@
 #define USE_THE_REPOSITORY_VARIABLE
 
 #include "test-tool.h"
+#include "blame.h"
 #include "commit-graph.h"
 #include "commit.h"
 #include "environment.h"
 #include "hex.h"
 #include "object.h"
 #include "repository.h"
+#include "revision.h"
 #include "setup.h"
 #include "tree.h"
 
@@ -65,11 +67,41 @@ static void test_get_commit_tree_in_graph(const char *gitdir,
 	repo_clear(&r);
 }
 
+static void test_blame_scoreboard(const char *path)
+{
+	struct repository *repo = the_repository;
+
+	setup_git_directory(repo);
+	/* A second setup exposes leaks left by the first scoreboard. */
+	for (int i = 0; i < 2; i++) {
+		const char *args[] = { "blame", "HEAD", NULL };
+		struct blame_scoreboard sb;
+		struct blame_origin *origin;
+		struct rev_info revs;
+
+		repo_init_revisions(repo, &revs, NULL);
+		setup_revisions(ARRAY_SIZE(args) - 1, args, &revs, NULL);
+		init_scoreboard(&sb);
+		sb.repo = repo;
+		sb.revs = &revs;
+		sb.path = path;
+		setup_scoreboard(&sb, &origin);
+		blame_origin_decref(origin);
+		cleanup_scoreboard(&sb);
+		release_revisions(&revs);
+	}
+	repo_clear(repo);
+}
+
 int cmd__repository(int argc, const char **argv)
 {
 	if (argc < 2)
 		die("must have at least 2 arguments");
-	if (!strcmp(argv[1], "parse_commit_in_graph")) {
+	if (!strcmp(argv[1], "blame-scoreboard")) {
+		if (argc != 3)
+			die("usage: test-tool repository blame-scoreboard <path>");
+		test_blame_scoreboard(argv[2]);
+	} else if (!strcmp(argv[1], "parse_commit_in_graph")) {
 		struct object_id oid;
 		if (argc < 5)
 			die("not enough arguments");
diff --git a/t/meson.build b/t/meson.build
index 3ca7b27104..6188a0c4bc 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -993,6 +993,7 @@ integration_tests = [
   't8013-blame-ignore-revs.sh',
   't8014-blame-ignore-fuzzy.sh',
   't8015-blame-diff-algorithm.sh',
+  't8016-blame-scoreboard.sh',
   't8020-last-modified.sh',
   't9001-send-email.sh',
   't9002-column.sh',
diff --git a/t/t8016-blame-scoreboard.sh b/t/t8016-blame-scoreboard.sh
new file mode 100755
index 0000000000..bc7f85c468
--- /dev/null
+++ b/t/t8016-blame-scoreboard.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='blame scoreboard teardown'
+
+. ./test-lib.sh
+
+test_expect_success 'set up and tear down two scoreboards in one process' '
+	test_commit one file &&
+	test-tool repository blame-scoreboard file
+'
+
+test_done

base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 
gitgitgadget

                 reply	other threads:[~2026-11-26  3:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=pull.2330.git.1795664230820.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ogunduz@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