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

[PATCH v2 01/24] builtin/replay: plug leaking `advance_name` variable

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 1, 2024, 10:38 UTC
Message-ID
<b5c8a9495ae81ba0757c92df634117e836e89699.1722499961.git.ps@pks.im>
In-Reply-To
<cover.1722499961.git.ps@pks.im>

The `advance_name` variable can either contain a static string when parsed via the `--advance` command line option or it may be an allocated string when set via `determine_replay_mode()`. Because we cannot be sure whether it is allocated or not we just didn't free it at all, resulting in a memory leak.

Split up the variables such that we can track the static and allocated strings separately and then free the allocated one to fix the memory leak.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/replay.c         | 20 ++++++++++++++------
 t/t3650-replay-basics.sh |  1 +
 2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/builtin/replay.c b/builtin/replay.c
index 0448326636..27b40eda98 100644
--- a/builtin/replay.c
+++ b/builtin/replay.c
@@ -151,7 +151,7 @@ static void get_ref_information(struct rev_cmdline_info *cmd_info,
 
 static void determine_replay_mode(struct rev_cmdline_info *cmd_info,
 				  const char *onto_name,
-				  const char **advance_name,
+				  char **advance_name,
 				  struct commit **onto,
 				  struct strset **update_refs)
 {
@@ -174,6 +174,7 @@ static void determine_replay_mode(struct rev_cmdline_info *cmd_info,
 		*onto = peel_committish(*advance_name);
 		if (repo_dwim_ref(the_repository, *advance_name, strlen(*advance_name),
 			     &oid, &fullname, 0) == 1) {
+			free(*advance_name);
 			*advance_name = fullname;
 		} else {
 			die(_("argument to --advance must be a reference"));
@@ -197,6 +198,7 @@ static void determine_replay_mode(struct rev_cmdline_info *cmd_info,
 		if (negative_refs_complete) {
 			struct hashmap_iter iter;
 			struct strmap_entry *entry;
+			const char *last_key = NULL;
 
 			if (rinfo.negative_refexprs == 0)
 				die(_("all positive revisions given must be references"));
@@ -208,8 +210,11 @@ static void determine_replay_mode(struct rev_cmdline_info *cmd_info,
 			/* Only one entry, but we have to loop to get it */
 			strset_for_each_entry(&rinfo.negative_refs,
 					      &iter, entry) {
-				*advance_name = entry->key;
+				last_key = entry->key;
 			}
+
+			free(*advance_name);
+			*advance_name = xstrdup_or_null(last_key);
 		} else { /* positive_refs_complete */
 			if (rinfo.negative_refexprs > 1)
 				die(_("cannot implicitly determine correct base for --onto"));
@@ -271,7 +276,8 @@ static struct commit *pick_regular_commit(struct commit *pickme,
 
 int cmd_replay(int argc, const char **argv, const char *prefix)
 {
-	const char *advance_name = NULL;
+	const char *advance_name_opt = NULL;
+	char *advance_name = NULL;
 	struct commit *onto = NULL;
 	const char *onto_name = NULL;
 	int contained = 0;
@@ -292,7 +298,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 		NULL
 	};
 	struct option replay_options[] = {
-		OPT_STRING(0, "advance", &advance_name,
+		OPT_STRING(0, "advance", &advance_name_opt,
 			   N_("branch"),
 			   N_("make replay advance given branch")),
 		OPT_STRING(0, "onto", &onto_name,
@@ -306,14 +312,15 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, replay_options, replay_usage,
 			     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
 
-	if (!onto_name && !advance_name) {
+	if (!onto_name && !advance_name_opt) {
 		error(_("option --onto or --advance is mandatory"));
 		usage_with_options(replay_usage, replay_options);
 	}
 
-	if (advance_name && contained)
+	if (advance_name_opt && contained)
 		die(_("options '%s' and '%s' cannot be used together"),
 		    "--advance", "--contained");
+	advance_name = xstrdup_or_null(advance_name_opt);
 
 	repo_init_revisions(the_repository, &revs, prefix);
 
@@ -441,6 +448,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
 
 cleanup:
 	release_revisions(&revs);
+	free(advance_name);
 
 	/* Return */
 	if (ret < 0)
diff --git a/t/t3650-replay-basics.sh b/t/t3650-replay-basics.sh
index 389670262e..12bd3db4cb 100755
--- a/t/t3650-replay-basics.sh
+++ b/t/t3650-replay-basics.sh
@@ -5,6 +5,7 @@ test_description='basic git replay tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 GIT_AUTHOR_NAME=author@name
-- 
2.46.0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 48 of 72 in “Memory leak fixes (pt.3)”
  1. 00/23 Memory leak fixes (pt.3)Patrick Steinhardt, Jul 26, 2024
  2. 01/23 builtin/replay: plug leaking `advance_name` variablePatrick Steinhardt, Jul 26, 2024
  3. Taylor BlauJul 31, 2024
  4. 02/23 builtin/log: fix leaking branch name when creating cover lettersPatrick Steinhardt, Jul 26, 2024
  5. Karthik NayakJul 30, 2024
  6. Taylor BlauJul 31, 2024
  7. 03/23 builtin/describe: fix memory leak with `--contains=`Patrick Steinhardt, Jul 26, 2024
  8. Karthik NayakJul 30, 2024
  9. Junio C HamanoJul 30, 2024
  10. Patrick SteinhardtJul 31, 2024
  11. Junio C HamanoJul 31, 2024
  12. Taylor BlauJul 31, 2024
  13. 04/23 builtin/describe: fix leaking array when running diff-indexPatrick Steinhardt, Jul 26, 2024
  14. Karthik NayakJul 30, 2024
  15. 05/23 builtin/describe: fix trivial memory leak when describing blobPatrick Steinhardt, Jul 26, 2024
  16. 06/23 builtin/name-rev: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  17. Junio C HamanoJul 30, 2024
  18. 07/23 builtin/submodule--helper: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  19. Rubén JustoJul 31, 2024
  20. Patrick SteinhardtAug 1, 2024
  21. 08/23 builtin/ls-remote: fix leaking `pattern` stringsPatrick Steinhardt, Jul 26, 2024
  22. Taylor BlauJul 31, 2024
  23. Patrick SteinhardtAug 1, 2024
  24. 09/23 builtin/remote: fix leaking strings in `branch_list`Patrick Steinhardt, Jul 26, 2024
  25. Taylor BlauJul 31, 2024
  26. 10/23 builtin/remote: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  27. 11/23 builtin/stash: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  28. Taylor BlauJul 31, 2024
  29. 12/23 builtin/rev-parse: fix memory leak with `--parseopt`Patrick Steinhardt, Jul 26, 2024
  30. Karthik NayakJul 30, 2024
  31. 13/23 builtin/show-branch: fix several memory leaksPatrick Steinhardt, Jul 26, 2024
  32. 14/23 builtin/credential-store: fix leaking credentialPatrick Steinhardt, Jul 26, 2024
  33. 15/23 builtin/rerere: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  34. 16/23 builtin/shortlog: fix various trivial memory leaksPatrick Steinhardt, Jul 26, 2024
  35. 17/23 builtin/worktree: fix leaking derived branch namesPatrick Steinhardt, Jul 26, 2024
  36. 18/23 builtin/credential-cache: fix trivial leaksPatrick Steinhardt, Jul 26, 2024
  37. 19/23 t/test-repository: fix leaking repositoryPatrick Steinhardt, Jul 26, 2024
  38. 20/23 object-name: fix leaking commit list itemsPatrick Steinhardt, Jul 26, 2024
  39. 21/23 entry: fix leaking pathnames during delayed checkoutPatrick Steinhardt, Jul 26, 2024
  40. 22/23 convert: fix leaking config stringsPatrick Steinhardt, Jul 26, 2024
  41. 23/23 commit-reach: fix trivial memory leak when computing reachabilityPatrick Steinhardt, Jul 26, 2024
  42. Karthik NayakJul 30, 2024
  43. Patrick SteinhardtJul 31, 2024
  44. Taylor BlauJul 31, 2024
  45. Patrick SteinhardtAug 1, 2024
  46. Taylor BlauAug 1, 2024
  47. 00/24 Memory leak fixes (pt.3)Patrick Steinhardt, Aug 1, 2024
  48. 01/24 builtin/replay: plug leaking `advance_name` variablePatrick Steinhardt, Aug 1, 2024
  49. 02/24 builtin/log: fix leaking branch name when creating cover lettersPatrick Steinhardt, Aug 1, 2024
  50. 03/24 builtin/describe: fix memory leak with `--contains=`Patrick Steinhardt, Aug 1, 2024
  51. 04/24 builtin/describe: fix leaking array when running diff-indexPatrick Steinhardt, Aug 1, 2024
  52. 05/24 builtin/describe: fix trivial memory leak when describing blobPatrick Steinhardt, Aug 1, 2024
  53. 06/24 builtin/name-rev: fix various trivial memory leaksPatrick Steinhardt, Aug 1, 2024
  54. 08/24 builtin/submodule--helper: fix leaking buffer in `is_tip_reachable`Patrick Steinhardt, Aug 1, 2024
  55. 09/24 builtin/ls-remote: fix leaking `pattern` stringsPatrick Steinhardt, Aug 1, 2024
  56. 10/24 builtin/remote: fix leaking strings in `branch_list`Patrick Steinhardt, Aug 1, 2024
  57. 11/24 builtin/remote: fix various trivial memory leaksPatrick Steinhardt, Aug 1, 2024
  58. 12/24 builtin/stash: fix various trivial memory leaksPatrick Steinhardt, Aug 1, 2024
  59. 13/24 builtin/rev-parse: fix memory leak with `--parseopt`Patrick Steinhardt, Aug 1, 2024
  60. 14/24 builtin/show-branch: fix several memory leaksPatrick Steinhardt, Aug 1, 2024
  61. 15/24 builtin/credential-store: fix leaking credentialPatrick Steinhardt, Aug 1, 2024
  62. 16/24 builtin/rerere: fix various trivial memory leaksPatrick Steinhardt, Aug 1, 2024
  63. 17/24 builtin/shortlog: fix various trivial memory leaksPatrick Steinhardt, Aug 1, 2024
  64. 18/24 builtin/worktree: fix leaking derived branch namesPatrick Steinhardt, Aug 1, 2024
  65. 19/24 builtin/credential-cache: fix trivial leaksPatrick Steinhardt, Aug 1, 2024
  66. 20/24 t/test-repository: fix leaking repositoryPatrick Steinhardt, Aug 1, 2024
  67. 21/24 object-name: fix leaking commit list itemsPatrick Steinhardt, Aug 1, 2024
  68. 22/24 entry: fix leaking pathnames during delayed checkoutPatrick Steinhardt, Aug 1, 2024
  69. 23/24 convert: fix leaking config stringsPatrick Steinhardt, Aug 1, 2024
  70. 24/24 commit-reach: fix trivial memory leak when computing reachabilityPatrick Steinhardt, Aug 1, 2024
  71. 07/24 builtin/submodule--helper: fix leaking clone depth parameterPatrick Steinhardt, Aug 1, 2024
  72. Taylor BlauAug 1, 2024

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.