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

[PATCH v2 21/24] object-name: fix leaking commit list items

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

When calling `get_oid_oneline()`, we pass in a `struct commit_list` that gets modified by the function. This creates a weird situation where the commit list may sometimes be empty after returning, but sometimes it will continue to carry additional commits. In those cases the remainder of the list leaks.

Ultimately, the design where we only pass partial ownership to `get_oid_oneline()` feels shoddy. Refactor the code such that we only pass a constant pointer to the list, creating a local copy as needed. Callers are thus always responsible for freeing the commit list, which then allows us to plug a bunch of memory leaks.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 object-name.c                | 26 ++++++++++++++++----------
 t/t1511-rev-parse-caret.sh   |  1 +
 t/t6133-pathspec-rev-dwim.sh |  2 ++
 3 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/object-name.c b/object-name.c
index 527b853ac4..240a93e7ce 100644
--- a/object-name.c
+++ b/object-name.c
@@ -27,7 +27,8 @@
 #include "date.h"
 #include "object-file-convert.h"
 
-static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);
+static int get_oid_oneline(struct repository *r, const char *, struct object_id *,
+			   const struct commit_list *);
 
 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
 
@@ -1254,6 +1255,8 @@ static int peel_onion(struct repository *r, const char *name, int len,
 		prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
 		commit_list_insert((struct commit *)o, &list);
 		ret = get_oid_oneline(r, prefix, oid, list);
+
+		free_commit_list(list);
 		free(prefix);
 		return ret;
 	}
@@ -1388,9 +1391,10 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
 
 static int get_oid_oneline(struct repository *r,
 			   const char *prefix, struct object_id *oid,
-			   struct commit_list *list)
+			   const struct commit_list *list)
 {
-	struct commit_list *backup = NULL, *l;
+	struct commit_list *copy = NULL;
+	const struct commit_list *l;
 	int found = 0;
 	int negative = 0;
 	regex_t regex;
@@ -1411,14 +1415,14 @@ static int get_oid_oneline(struct repository *r,
 
 	for (l = list; l; l = l->next) {
 		l->item->object.flags |= ONELINE_SEEN;
-		commit_list_insert(l->item, &backup);
+		commit_list_insert(l->item, &copy);
 	}
-	while (list) {
+	while (copy) {
 		const char *p, *buf;
 		struct commit *commit;
 		int matches;
 
-		commit = pop_most_recent_commit(&list, ONELINE_SEEN);
+		commit = pop_most_recent_commit(&copy, ONELINE_SEEN);
 		if (!parse_object(r, &commit->object.oid))
 			continue;
 		buf = repo_get_commit_buffer(r, commit, NULL);
@@ -1433,10 +1437,9 @@ static int get_oid_oneline(struct repository *r,
 		}
 	}
 	regfree(&regex);
-	free_commit_list(list);
-	for (l = backup; l; l = l->next)
+	for (l = list; l; l = l->next)
 		clear_commit_marks(l->item, ONELINE_SEEN);
-	free_commit_list(backup);
+	free_commit_list(copy);
 	return found ? 0 : -1;
 }
 
@@ -2024,7 +2027,10 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
 			refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
 			refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
 			commit_list_sort_by_date(&list);
-			return get_oid_oneline(repo, name + 2, oid, list);
+			ret = get_oid_oneline(repo, name + 2, oid, list);
+
+			free_commit_list(list);
+			return ret;
 		}
 		if (namelen < 3 ||
 		    name[2] != ':' ||
diff --git a/t/t1511-rev-parse-caret.sh b/t/t1511-rev-parse-caret.sh
index 6ecfed86bc..e7e78a4028 100755
--- a/t/t1511-rev-parse-caret.sh
+++ b/t/t1511-rev-parse-caret.sh
@@ -5,6 +5,7 @@ test_description='tests for ref^{stuff}'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
diff --git a/t/t6133-pathspec-rev-dwim.sh b/t/t6133-pathspec-rev-dwim.sh
index a290ffca0d..6dd4bbbf9f 100755
--- a/t/t6133-pathspec-rev-dwim.sh
+++ b/t/t6133-pathspec-rev-dwim.sh
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test dwim of revs versus pathspecs in revision parser'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-- 
2.46.0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 67 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.