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

[PATCH v2 08/11] checkout: avoid "struct unpack_trees_options" leak

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 1, 2022, 10:42 UTC
Message-ID
<patch-v2-08.11-e21d7e4e9df-20220701T104017Z-avarab@gmail.com>
In-Reply-To
<cover-v2-00.11-00000000000-20220701T104017Z-avarab@gmail.com>

In 1c41d2805e4 (unpack_trees_options: free messages when done, 2018-05-21) we started calling clear_unpack_trees_porcelain() on this codepath, but missed this error path.

We could call clear_unpack_trees_porcelain() just before we error() and return when unmerged_cache() fails, but the more correct fix is to not have the unmerged_cache() check happen in the middle of our "topts" setup.

Before 23cbf11b5c0 (merge-recursive: porcelain messages for checkout, 2010-08-11) we would not malloc() to setup our "topts", which is when this started to leak on the error path.

Before that this code wasn't conflating the setup of "topts" and the unmerged_cache() call in any meaningful way. The initial version in 782c2d65c24 (Build in checkout, 2008-02-07) just does a "memset" of it, and initializes a single struct member.

Then in 8ccba008ee3 (unpack-trees: allow Porcelain to give different error messages, 2008-05-17) we added the initialization of the error message, which as noted above finally started leaking in 23cbf11b5c0.

Let's fix the memory leak, and avoid future issues by initializing the "topts" with a helper function. There are no functional changes here.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/checkout.c | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2eefda81d8c..1109f1301f4 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -710,6 +710,26 @@ static void setup_branch_path(struct branch_info *branch)
 	branch->path = strbuf_detach(&buf, NULL);
 }
 
+static void init_topts(struct unpack_trees_options *topts, int merge,
+		       int show_progress, int overwrite_ignore,
+		       struct commit *old_commit)
+{
+	memset(topts, 0, sizeof(*topts));
+	topts->head_idx = -1;
+	topts->src_index = &the_index;
+	topts->dst_index = &the_index;
+
+	setup_unpack_trees_porcelain(topts, "checkout");
+
+	topts->initial_checkout = is_cache_unborn();
+	topts->update = 1;
+	topts->merge = 1;
+	topts->quiet = merge && old_commit;
+	topts->verbose_update = show_progress;
+	topts->fn = twoway_merge;
+	topts->preserve_ignored = !overwrite_ignore;
+}
+
 static int merge_working_tree(const struct checkout_opts *opts,
 			      struct branch_info *old_branch_info,
 			      struct branch_info *new_branch_info,
@@ -740,13 +760,6 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		struct unpack_trees_options topts;
 		const struct object_id *old_commit_oid;
 
-		memset(&topts, 0, sizeof(topts));
-		topts.head_idx = -1;
-		topts.src_index = &the_index;
-		topts.dst_index = &the_index;
-
-		setup_unpack_trees_porcelain(&topts, "checkout");
-
 		refresh_cache(REFRESH_QUIET);
 
 		if (unmerged_cache()) {
@@ -755,17 +768,12 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		}
 
 		/* 2-way merge to the new branch */
-		topts.initial_checkout = is_cache_unborn();
-		topts.update = 1;
-		topts.merge = 1;
-		topts.quiet = opts->merge && old_branch_info->commit;
-		topts.verbose_update = opts->show_progress;
-		topts.fn = twoway_merge;
+		init_topts(&topts, opts->merge, opts->show_progress,
+			   opts->overwrite_ignore, old_branch_info->commit);
 		init_checkout_metadata(&topts.meta, new_branch_info->refname,
 				       new_branch_info->commit ?
 				       &new_branch_info->commit->object.oid :
 				       &new_branch_info->oid, NULL);
-		topts.preserve_ignored = !opts->overwrite_ignore;
 
 		old_commit_oid = old_branch_info->commit ?
 			&old_branch_info->commit->object.oid :
-- 
2.37.0.900.g4d0de1cceb2
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 26 of 31 in “built-ins: fix common memory leaks”
  1. 00/11 built-ins: fix common memory leaksÆvar Arnfjörð Bjarmason, Jun 30, 2022
  2. 01/11 check-ref-format: fix trivial memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  3. Junio C HamanoJun 30, 2022
  4. 02/11 clone: fix memory leak in copy_ref() callÆvar Arnfjörð Bjarmason, Jun 30, 2022
  5. Junio C HamanoJun 30, 2022
  6. 03/11 submodule.c: free() memory from xgetcwd()Ævar Arnfjörð Bjarmason, Jun 30, 2022
  7. 04/11 revert: free "struct replay_opts" membersÆvar Arnfjörð Bjarmason, Jun 30, 2022
  8. 05/11 cat-file: fix a memory leak in --batch-command modeÆvar Arnfjörð Bjarmason, Jun 30, 2022
  9. 06/11 merge-file: refactor for subsequent memory leak fixÆvar Arnfjörð Bjarmason, Jun 30, 2022
  10. 07/11 merge-file: fix memory leaks on error pathÆvar Arnfjörð Bjarmason, Jun 30, 2022
  11. 08/11 checkout: add a missing clear_unpack_trees_porcelain()Ævar Arnfjörð Bjarmason, Jun 30, 2022
  12. Junio C HamanoJun 30, 2022
  13. Ævar Arnfjörð BjarmasonJun 30, 2022
  14. Junio C HamanoJun 30, 2022
  15. 09/11 gc: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  16. 10/11 cat-file: fix a common "struct object_context" memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  17. 11/11 pull: fix a "struct oid_array" memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  18. 00/11 built-ins: fix common memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  19. 01/11 check-ref-format: fix trivial memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  20. 02/11 clone: fix memory leak in wanted_peer_refs()Ævar Arnfjörð Bjarmason, Jul 1, 2022
  21. 03/11 submodule.c: free() memory from xgetcwd()Ævar Arnfjörð Bjarmason, Jul 1, 2022
  22. 04/11 revert: free "struct replay_opts" membersÆvar Arnfjörð Bjarmason, Jul 1, 2022
  23. 05/11 cat-file: fix a memory leak in --batch-command modeÆvar Arnfjörð Bjarmason, Jul 1, 2022
  24. 06/11 merge-file: refactor for subsequent memory leak fixÆvar Arnfjörð Bjarmason, Jul 1, 2022
  25. 09/11 gc: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  26. 08/11 checkout: avoid "struct unpack_trees_options" leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  27. Junio C HamanoJul 1, 2022
  28. 07/11 merge-file: fix memory leaks on error pathÆvar Arnfjörð Bjarmason, Jul 1, 2022
  29. 10/11 cat-file: fix a common "struct object_context" memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  30. 11/11 pull: fix a "struct oid_array" memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  31. Junio C HamanoJul 1, 2022

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.