git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood123@gmail.com>,
	Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH v2 12/29] builtin/merge-recursive: fix leaking object ID bases
Date: Tue, 11 Jun 2024 11:20:09 +0200	[thread overview]
Message-ID: <fb30a254abaf132aa3e44b1edea927cf91e1d86f.1718095906.git.ps@pks.im> (raw)
In-Reply-To: <cover.1718095906.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 5662 bytes --]

In `cmd_merge_recursive()` we have a static array of object ID bases
that we pass to `merge_recursive_generic()`. This interface is somewhat
weird though because the latter function accepts a pointer to a pointer
of object IDs, which requires us to allocate the object IDs on the heap.
And as we never free those object IDs, the end result is a leak.

While we can easily solve this leak by just freeing the respective
object IDs, the whole calling convention is somewhat weird. Instead,
refactor `merge_recursive_generic()` to accept a plain pointer to object
IDs so that we can avoid allocating them altogether.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/am.c                              | 6 +++---
 builtin/merge-recursive.c                 | 6 ++----
 merge-recursive.c                         | 8 ++++----
 merge-recursive.h                         | 2 +-
 t/t6432-merge-recursive-space-options.sh  | 1 +
 t/t6434-merge-recursive-rename-options.sh | 1 +
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 36839029d2..4ba44e2d70 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1573,8 +1573,8 @@ static int build_fake_ancestor(const struct am_state *state, const char *index_f
  */
 static int fall_back_threeway(const struct am_state *state, const char *index_path)
 {
-	struct object_id orig_tree, their_tree, our_tree;
-	const struct object_id *bases[1] = { &orig_tree };
+	struct object_id their_tree, our_tree;
+	struct object_id bases[1] = { 0 };
 	struct merge_options o;
 	struct commit *result;
 	char *their_tree_name;
@@ -1588,7 +1588,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
 	discard_index(the_repository->index);
 	read_index_from(the_repository->index, index_path, get_git_dir());
 
-	if (write_index_as_tree(&orig_tree, the_repository->index, index_path, 0, NULL))
+	if (write_index_as_tree(&bases[0], the_repository->index, index_path, 0, NULL))
 		return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
 
 	say(state, stdout, _("Using index info to reconstruct a base tree..."));
diff --git a/builtin/merge-recursive.c b/builtin/merge-recursive.c
index c2ce044a20..82bebea15b 100644
--- a/builtin/merge-recursive.c
+++ b/builtin/merge-recursive.c
@@ -23,7 +23,7 @@ static char *better_branch_name(const char *branch)
 
 int cmd_merge_recursive(int argc, const char **argv, const char *prefix UNUSED)
 {
-	const struct object_id *bases[21];
+	struct object_id bases[21];
 	unsigned bases_count = 0;
 	int i, failed;
 	struct object_id h1, h2;
@@ -49,10 +49,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix UNUSED)
 			continue;
 		}
 		if (bases_count < ARRAY_SIZE(bases)-1) {
-			struct object_id *oid = xmalloc(sizeof(struct object_id));
-			if (repo_get_oid(the_repository, argv[i], oid))
+			if (repo_get_oid(the_repository, argv[i], &bases[bases_count++]))
 				die(_("could not parse object '%s'"), argv[i]);
-			bases[bases_count++] = oid;
 		}
 		else
 			warning(Q_("cannot handle more than %d base. "
diff --git a/merge-recursive.c b/merge-recursive.c
index 8c8e8b4868..eff73dac02 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -3866,7 +3866,7 @@ int merge_recursive_generic(struct merge_options *opt,
 			    const struct object_id *head,
 			    const struct object_id *merge,
 			    int num_merge_bases,
-			    const struct object_id **merge_bases,
+			    const struct object_id *merge_bases,
 			    struct commit **result)
 {
 	int clean;
@@ -3879,10 +3879,10 @@ int merge_recursive_generic(struct merge_options *opt,
 		int i;
 		for (i = 0; i < num_merge_bases; ++i) {
 			struct commit *base;
-			if (!(base = get_ref(opt->repo, merge_bases[i],
-					     oid_to_hex(merge_bases[i]))))
+			if (!(base = get_ref(opt->repo, &merge_bases[i],
+					     oid_to_hex(&merge_bases[i]))))
 				return err(opt, _("Could not parse object '%s'"),
-					   oid_to_hex(merge_bases[i]));
+					   oid_to_hex(&merge_bases[i]));
 			commit_list_insert(base, &ca);
 		}
 		if (num_merge_bases == 1)
diff --git a/merge-recursive.h b/merge-recursive.h
index e67d38c303..839eb6436e 100644
--- a/merge-recursive.h
+++ b/merge-recursive.h
@@ -123,7 +123,7 @@ int merge_recursive_generic(struct merge_options *opt,
 			    const struct object_id *head,
 			    const struct object_id *merge,
 			    int num_merge_bases,
-			    const struct object_id **merge_bases,
+			    const struct object_id *merge_bases,
 			    struct commit **result);
 
 #endif
diff --git a/t/t6432-merge-recursive-space-options.sh b/t/t6432-merge-recursive-space-options.sh
index db4b77e63d..c93538b0c3 100755
--- a/t/t6432-merge-recursive-space-options.sh
+++ b/t/t6432-merge-recursive-space-options.sh
@@ -14,6 +14,7 @@ test_description='merge-recursive space options
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
diff --git a/t/t6434-merge-recursive-rename-options.sh b/t/t6434-merge-recursive-rename-options.sh
index a11707835b..df1d0c156c 100755
--- a/t/t6434-merge-recursive-rename-options.sh
+++ b/t/t6434-merge-recursive-rename-options.sh
@@ -29,6 +29,7 @@ mentions this in a different context).
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 get_expected_stages () {
-- 
2.45.2.436.gcd77e87115.dirty


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2024-06-11  9:20 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03  9:46 [PATCH 00/29] Memory leak fixes (pt.2) Patrick Steinhardt
2024-06-03  9:46 ` [PATCH 01/29] revision: fix memory leak when reversing revisions Patrick Steinhardt
2024-06-03  9:46 ` [PATCH 02/29] parse-options: fix leaks for users of OPT_FILENAME Patrick Steinhardt
2024-06-06 10:00   ` Karthik Nayak
2024-06-03  9:46 ` [PATCH 03/29] notes-utils: free note trees when releasing copied notes Patrick Steinhardt
2024-06-06 10:02   ` Karthik Nayak
2024-06-03  9:46 ` [PATCH 04/29] bundle: plug leaks in `create_bundle()` Patrick Steinhardt
2024-06-03  9:46 ` [PATCH 05/29] biultin/rev-parse: fix memory leaks in `--parseopt` mode Patrick Steinhardt
2024-06-03  9:46 ` [PATCH 06/29] merge-recursive: fix leaging rename conflict info Patrick Steinhardt
2024-06-06 10:07   ` Karthik Nayak
2024-06-03  9:46 ` [PATCH 07/29] revision: fix leaking display notes Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 08/29] notes: fix memory leak when pruning notes Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 09/29] builtin/rev-list: fix leaking bitmap index when calculating disk usage Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 10/29] object-name: free leaking object contexts Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 11/29] builtin/difftool: plug memory leaks in `run_dir_diff()` Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 12/29] builtin/merge-recursive: fix leaking object ID bases Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 13/29] merge-recursive: fix memory leak when finalizing merge Patrick Steinhardt
2024-06-06 10:50   ` Karthik Nayak
2024-06-06 15:52     ` Phillip Wood
2024-06-12  9:33       ` Karthik Nayak
2024-06-03  9:47 ` [PATCH 14/29] builtin/log: fix leaking commit list in git-cherry(1) Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 15/29] revision: free diff options Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 16/29] builtin/stash: fix leak in `show_stash()` Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 17/29] rerere: fix various trivial leaks Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 18/29] config: fix leaking "core.notesref" variable Patrick Steinhardt
2024-06-03  9:47 ` [PATCH 19/29] commit: fix leaking parents when calling `commit_tree_extended()` Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 20/29] sequencer: fix leaking string buffer in `commit_staged_changes()` Patrick Steinhardt
2024-06-03 13:14   ` Phillip Wood
2024-06-04  6:45     ` Patrick Steinhardt
2024-06-04  7:19       ` Patrick Steinhardt
2024-06-04 13:58         ` Phillip Wood
2024-06-03  9:48 ` [PATCH 21/29] apply: fix leaking string in `match_fragment()` Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 22/29] builtin/clone: plug leaking HEAD ref in `wanted_peer_refs()` Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 23/29] sequencer: fix memory leaks in `make_script_with_merges()` Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 24/29] builtin/merge: fix leaking `struct cmdnames` in `get_strategy()` Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 25/29] merge: fix leaking merge bases Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 26/29] line-range: plug leaking find functions Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 27/29] blame: fix leaking data for blame scoreboards Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 28/29] builtin/blame: fix leaking prefixed paths Patrick Steinhardt
2024-06-03  9:48 ` [PATCH 29/29] builtin/blame: fix leaking ignore revs files Patrick Steinhardt
2024-06-06 14:33 ` [PATCH 00/29] Memory leak fixes (pt.2) Karthik Nayak
2024-06-07  4:07   ` Patrick Steinhardt
2024-06-11  9:19 ` [PATCH v2 " Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 01/29] revision: fix memory leak when reversing revisions Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 02/29] parse-options: fix leaks for users of OPT_FILENAME Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 03/29] notes-utils: free note trees when releasing copied notes Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 04/29] bundle: plug leaks in `create_bundle()` Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 05/29] biultin/rev-parse: fix memory leaks in `--parseopt` mode Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 06/29] merge-recursive: fix leaking rename conflict info Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 07/29] revision: fix leaking display notes Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 08/29] notes: fix memory leak when pruning notes Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 09/29] builtin/rev-list: fix leaking bitmap index when calculating disk usage Patrick Steinhardt
2024-06-11  9:19   ` [PATCH v2 10/29] object-name: free leaking object contexts Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 11/29] builtin/difftool: plug memory leaks in `run_dir_diff()` Patrick Steinhardt
2024-06-11  9:20   ` Patrick Steinhardt [this message]
2024-06-11  9:20   ` [PATCH v2 13/29] merge-recursive: fix memory leak when finalizing merge Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 14/29] builtin/log: fix leaking commit list in git-cherry(1) Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 15/29] revision: free diff options Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 16/29] builtin/stash: fix leak in `show_stash()` Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 17/29] rerere: fix various trivial leaks Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 18/29] config: fix leaking "core.notesref" variable Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 19/29] commit: fix leaking parents when calling `commit_tree_extended()` Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 20/29] sequencer: fix leaking string buffer in `commit_staged_changes()` Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 21/29] apply: fix leaking string in `match_fragment()` Patrick Steinhardt
2024-06-11  9:20   ` [PATCH v2 22/29] builtin/clone: plug leaking HEAD ref in `wanted_peer_refs()` Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 23/29] sequencer: fix memory leaks in `make_script_with_merges()` Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 24/29] builtin/merge: fix leaking `struct cmdnames` in `get_strategy()` Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 25/29] merge: fix leaking merge bases Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 26/29] line-range: plug leaking find functions Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 27/29] blame: fix leaking data for blame scoreboards Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 28/29] builtin/blame: fix leaking prefixed paths Patrick Steinhardt
2024-06-11  9:21   ` [PATCH v2 29/29] builtin/blame: fix leaking ignore revs files Patrick Steinhardt
2024-06-12  9:09   ` [PATCH v2 00/29] Memory leak fixes (pt.2) Phillip Wood
2024-06-12  9:35   ` Karthik Nayak

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=fb30a254abaf132aa3e44b1edea927cf91e1d86f.1718095906.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    --cc=phillip.wood123@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).