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

[PATCH v4 13/22] builtin/fast-export: plug leaking tag names

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 14, 2024, 06:52 UTC
Message-ID
<b4096e971fe2fb3c1a6d5f763e08dc62eb6f3d38.1723614263.git.ps@pks.im>
In-Reply-To
<cover.1723614263.git.ps@pks.im>

When resolving revisions in `get_tags_and_duplicates()`, we only partially manage the lifetime of `full_name`. In fact, managing its lifetime properly is almost impossible because we put direct pointers to that variable into multiple lists without duplicating the string. The consequence is that these strings will ultimately leak.

Refactor the code to make the lists we put those names into duplicate the memory. This allows us to properly free the string as required and thus plugs the memory leak.

While this requires us to allocate more data overall, it shouldn't be all that bad given that the number of allocations corresponds with the number of command line parameters, which typically aren't all that many.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/fast-export.c            | 17 ++++++++++++-----
 t/t9351-fast-export-anonymize.sh |  1 +
 2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index fe92d2436c..f253b79322 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -42,8 +42,8 @@ static int full_tree;
 static int reference_excluded_commits;
 static int show_original_ids;
 static int mark_tags;
-static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
-static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
+static struct string_list extra_refs = STRING_LIST_INIT_DUP;
+static struct string_list tag_refs = STRING_LIST_INIT_DUP;
 static struct refspec refspecs = REFSPEC_INIT_FETCH;
 static int anonymize;
 static struct hashmap anonymized_seeds;
@@ -901,7 +901,7 @@ static void handle_tag(const char *name, struct tag *tag)
 	free(buf);
 }
 
-static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
+static struct commit *get_commit(struct rev_cmdline_entry *e, const char *full_name)
 {
 	switch (e->item->type) {
 	case OBJ_COMMIT:
@@ -932,14 +932,16 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
 		struct rev_cmdline_entry *e = info->rev + i;
 		struct object_id oid;
 		struct commit *commit;
-		char *full_name;
+		char *full_name = NULL;
 
 		if (e->flags & UNINTERESTING)
 			continue;
 
 		if (repo_dwim_ref(the_repository, e->name, strlen(e->name),
-				  &oid, &full_name, 0) != 1)
+				  &oid, &full_name, 0) != 1) {
+			free(full_name);
 			continue;
+		}
 
 		if (refspecs.nr) {
 			char *private;
@@ -955,6 +957,7 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
 			warning("%s: Unexpected object of type %s, skipping.",
 				e->name,
 				type_name(e->item->type));
+			free(full_name);
 			continue;
 		}
 
@@ -963,10 +966,12 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
 			break;
 		case OBJ_BLOB:
 			export_blob(&commit->object.oid);
+			free(full_name);
 			continue;
 		default: /* OBJ_TAG (nested tags) is already handled */
 			warning("Tag points to object of unexpected type %s, skipping.",
 				type_name(commit->object.type));
+			free(full_name);
 			continue;
 		}
 
@@ -979,6 +984,8 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
 
 		if (!*revision_sources_at(&revision_sources, commit))
 			*revision_sources_at(&revision_sources, commit) = full_name;
+		else
+			free(full_name);
 	}
 
 	string_list_sort(&extra_refs);
diff --git a/t/t9351-fast-export-anonymize.sh b/t/t9351-fast-export-anonymize.sh
index 156a647484..c0d9d7be75 100755
--- a/t/t9351-fast-export-anonymize.sh
+++ b/t/t9351-fast-export-anonymize.sh
@@ -4,6 +4,7 @@ test_description='basic tests for fast-export --anonymize'
 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 simple repo' '
-- 
2.46.0.46.g406f326d27.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 137 of 146 in “Memory leak fixes (pt.4)”
  1. 00/22 Memory leak fixes (pt.4)Patrick Steinhardt, Aug 6, 2024
  2. 01/22 remote: plug memory leak when aliasing URLsPatrick Steinhardt, Aug 6, 2024
  3. 02/22 git: fix leaking system pathsPatrick Steinhardt, Aug 6, 2024
  4. James LiuAug 7, 2024
  5. 03/22 object-file: fix memory leak when reading corrupted headersPatrick Steinhardt, Aug 6, 2024
  6. 04/22 object-name: fix leaking symlink paths in object contextPatrick Steinhardt, Aug 6, 2024
  7. 05/22 bulk-checkin: fix leaking state TODOPatrick Steinhardt, Aug 6, 2024
  8. 06/22 read-cache: fix leaking hashfile when writing index failsPatrick Steinhardt, Aug 6, 2024
  9. James LiuAug 7, 2024
  10. Patrick SteinhardtAug 8, 2024
  11. 07/22 submodule-config: fix leaking name enrty when traversing submodulesPatrick Steinhardt, Aug 6, 2024
  12. 08/22 config: fix leaking comment character configPatrick Steinhardt, Aug 6, 2024
  13. James LiuAug 7, 2024
  14. Patrick SteinhardtAug 8, 2024
  15. Junio C HamanoAug 8, 2024
  16. 09/22 builtin/rebase: fix leaking `commit.gpgsign` valuePatrick Steinhardt, Aug 6, 2024
  17. James LiuAug 7, 2024
  18. Patrick SteinhardtAug 8, 2024
  19. Phillip WoodAug 8, 2024
  20. Patrick SteinhardtAug 8, 2024
  21. 10/22 builtin/notes: fix leaking `struct notes_tree` when merging notesPatrick Steinhardt, Aug 6, 2024
  22. 11/22 builtin/fast-import: plug trivial memory leaksPatrick Steinhardt, Aug 6, 2024
  23. 12/22 builtin/fast-export: fix leaking diff optionsPatrick Steinhardt, Aug 6, 2024
  24. 13/22 builtin/fast-export: plug leaking tag namesPatrick Steinhardt, Aug 6, 2024
  25. James LiuAug 7, 2024
  26. Patrick SteinhardtAug 8, 2024
  27. 14/22 merge-ort: unconditionally release attributes indexPatrick Steinhardt, Aug 6, 2024
  28. 15/22 sequencer: release todo list on error pathsPatrick Steinhardt, Aug 6, 2024
  29. Phillip WoodAug 8, 2024
  30. Junio C HamanoAug 8, 2024
  31. 16/22 unpack-trees: clear index when not propagating itPatrick Steinhardt, Aug 6, 2024
  32. 17/22 diff: fix leak when parsing invalid ignore regex optionPatrick Steinhardt, Aug 6, 2024
  33. 18/22 builtin/format-patch: fix various trivial memory leaksPatrick Steinhardt, Aug 6, 2024
  34. James LiuAug 7, 2024
  35. Patrick SteinhardtAug 8, 2024
  36. 19/22 userdiff: fix leaking memory for configured diff driversPatrick Steinhardt, Aug 6, 2024
  37. James LiuAug 7, 2024
  38. Patrick SteinhardtAug 8, 2024
  39. Junio C HamanoAug 8, 2024
  40. 20/22 builtin/log: fix leak when showing converted blob contentsPatrick Steinhardt, Aug 6, 2024
  41. 21/22 diff: free state populated via optionsPatrick Steinhardt, Aug 6, 2024
  42. 22/22 builtin/diff: free symmetric diff membersPatrick Steinhardt, Aug 6, 2024
  43. James LiuAug 7, 2024
  44. Patrick SteinhardtAug 8, 2024
  45. James LiuAug 8, 2024
  46. Junio C HamanoAug 7, 2024
  47. Patrick SteinhardtAug 7, 2024
  48. Junio C HamanoAug 8, 2024
  49. 00/22 Memory leak fixes (pt.4)Patrick Steinhardt, Aug 8, 2024
  50. 01/22 remote: plug memory leak when aliasing URLsPatrick Steinhardt, Aug 8, 2024
  51. karthik nayakAug 12, 2024
  52. Taylor BlauAug 12, 2024
  53. Jeff KingAug 12, 2024
  54. Patrick SteinhardtAug 13, 2024
  55. 02/22 git: fix leaking system pathsPatrick Steinhardt, Aug 8, 2024
  56. Taylor BlauAug 12, 2024
  57. Patrick SteinhardtAug 13, 2024
  58. Junio C HamanoAug 13, 2024
  59. 03/22 object-file: fix memory leak when reading corrupted headersPatrick Steinhardt, Aug 8, 2024
  60. karthik nayakAug 12, 2024
  61. 04/22 object-name: fix leaking symlink paths in object contextPatrick Steinhardt, Aug 8, 2024
  62. 05/22 bulk-checkin: fix leaking state TODOPatrick Steinhardt, Aug 8, 2024
  63. 06/22 read-cache: fix leaking hashfile when writing index failsPatrick Steinhardt, Aug 8, 2024
  64. 07/22 submodule-config: fix leaking name enrty when traversing submodulesPatrick Steinhardt, Aug 8, 2024
  65. 08/22 config: fix leaking comment character configPatrick Steinhardt, Aug 8, 2024
  66. Junio C HamanoAug 8, 2024
  67. Patrick SteinhardtAug 12, 2024
  68. Junio C HamanoAug 12, 2024
  69. Patrick SteinhardtAug 13, 2024
  70. 09/22 builtin/rebase: fix leaking `commit.gpgsign` valuePatrick Steinhardt, Aug 8, 2024
  71. 10/22 builtin/notes: fix leaking `struct notes_tree` when merging notesPatrick Steinhardt, Aug 8, 2024
  72. 11/22 builtin/fast-import: plug trivial memory leaksPatrick Steinhardt, Aug 8, 2024
  73. 12/22 builtin/fast-export: fix leaking diff optionsPatrick Steinhardt, Aug 8, 2024
  74. karthik nayakAug 12, 2024
  75. 13/22 builtin/fast-export: plug leaking tag namesPatrick Steinhardt, Aug 8, 2024
  76. 14/22 merge-ort: unconditionally release attributes indexPatrick Steinhardt, Aug 8, 2024
  77. 15/22 sequencer: release todo list on error pathsPatrick Steinhardt, Aug 8, 2024
  78. 16/22 unpack-trees: clear index when not propagating itPatrick Steinhardt, Aug 8, 2024
  79. 17/22 diff: fix leak when parsing invalid ignore regex optionPatrick Steinhardt, Aug 8, 2024
  80. 18/22 builtin/format-patch: fix various trivial memory leaksPatrick Steinhardt, Aug 8, 2024
  81. 19/22 userdiff: fix leaking memory for configured diff driversPatrick Steinhardt, Aug 8, 2024
  82. 20/22 builtin/log: fix leak when showing converted blob contentsPatrick Steinhardt, Aug 8, 2024
  83. 21/22 diff: free state populated via optionsPatrick Steinhardt, Aug 8, 2024
  84. 22/22 builtin/diff: free symmetric diff membersPatrick Steinhardt, Aug 8, 2024
  85. karthik nayakAug 12, 2024
  86. karthik nayakAug 12, 2024
  87. Junio C HamanoAug 12, 2024
  88. Patrick SteinhardtAug 13, 2024
  89. Phillip WoodAug 12, 2024
  90. Junio C HamanoAug 12, 2024
  91. 00/22 Memory leak fixes (pt.4)Patrick Steinhardt, Aug 13, 2024
  92. 01/22 remote: plug memory leak when aliasing URLsPatrick Steinhardt, Aug 13, 2024
  93. 02/22 git: fix leaking system pathsPatrick Steinhardt, Aug 13, 2024
  94. 03/22 object-file: fix memory leak when reading corrupted headersPatrick Steinhardt, Aug 13, 2024
  95. 04/22 object-name: fix leaking symlink paths in object contextPatrick Steinhardt, Aug 13, 2024
  96. 05/22 bulk-checkin: fix leaking state TODOPatrick Steinhardt, Aug 13, 2024
  97. 06/22 read-cache: fix leaking hashfile when writing index failsPatrick Steinhardt, Aug 13, 2024
  98. 07/22 submodule-config: fix leaking name entry when traversing submodulesPatrick Steinhardt, Aug 13, 2024
  99. 08/22 config: fix leaking comment character configPatrick Steinhardt, Aug 13, 2024
  100. 09/22 builtin/rebase: fix leaking `commit.gpgsign` valuePatrick Steinhardt, Aug 13, 2024
  101. 10/22 builtin/notes: fix leaking `struct notes_tree` when merging notesPatrick Steinhardt, Aug 13, 2024
  102. 11/22 builtin/fast-import: plug trivial memory leaksPatrick Steinhardt, Aug 13, 2024
  103. 12/22 builtin/fast-export: fix leaking diff optionsPatrick Steinhardt, Aug 13, 2024
  104. Junio C HamanoAug 13, 2024
  105. Patrick SteinhardtAug 14, 2024
  106. 13/22 builtin/fast-export: plug leaking tag namesPatrick Steinhardt, Aug 13, 2024
  107. 14/22 merge-ort: unconditionally release attributes indexPatrick Steinhardt, Aug 13, 2024
  108. 15/22 sequencer: release todo list on error pathsPatrick Steinhardt, Aug 13, 2024
  109. 16/22 unpack-trees: clear index when not propagating itPatrick Steinhardt, Aug 13, 2024
  110. 17/22 diff: fix leak when parsing invalid ignore regex optionPatrick Steinhardt, Aug 13, 2024
  111. 18/22 builtin/format-patch: fix various trivial memory leaksPatrick Steinhardt, Aug 13, 2024
  112. Junio C HamanoAug 13, 2024
  113. Patrick SteinhardtAug 14, 2024
  114. Junio C HamanoAug 13, 2024
  115. 19/22 userdiff: fix leaking memory for configured diff driversPatrick Steinhardt, Aug 13, 2024
  116. 20/22 builtin/log: fix leak when showing converted blob contentsPatrick Steinhardt, Aug 13, 2024
  117. 21/22 diff: free state populated via optionsPatrick Steinhardt, Aug 13, 2024
  118. Junio C HamanoAug 13, 2024
  119. 22/22 builtin/diff: free symmetric diff membersPatrick Steinhardt, Aug 13, 2024
  120. Junio C HamanoAug 13, 2024
  121. Patrick SteinhardtAug 14, 2024
  122. Junio C HamanoAug 14, 2024
  123. Junio C HamanoAug 13, 2024
  124. 00/22 Memory leak fixes (pt.4)Patrick Steinhardt, Aug 14, 2024
  125. 01/22 remote: plug memory leak when aliasing URLsPatrick Steinhardt, Aug 14, 2024
  126. 02/22 git: fix leaking system pathsPatrick Steinhardt, Aug 14, 2024
  127. 03/22 object-file: fix memory leak when reading corrupted headersPatrick Steinhardt, Aug 14, 2024
  128. 04/22 object-name: fix leaking symlink paths in object contextPatrick Steinhardt, Aug 14, 2024
  129. 05/22 bulk-checkin: fix leaking state TODOPatrick Steinhardt, Aug 14, 2024
  130. 06/22 read-cache: fix leaking hashfile when writing index failsPatrick Steinhardt, Aug 14, 2024
  131. 07/22 submodule-config: fix leaking name entry when traversing submodulesPatrick Steinhardt, Aug 14, 2024
  132. 08/22 config: fix leaking comment character configPatrick Steinhardt, Aug 14, 2024
  133. 09/22 builtin/rebase: fix leaking `commit.gpgsign` valuePatrick Steinhardt, Aug 14, 2024
  134. 10/22 builtin/notes: fix leaking `struct notes_tree` when merging notesPatrick Steinhardt, Aug 14, 2024
  135. 11/22 builtin/fast-import: plug trivial memory leaksPatrick Steinhardt, Aug 14, 2024
  136. 12/22 builtin/fast-export: fix leaking diff optionsPatrick Steinhardt, Aug 14, 2024
  137. 13/22 builtin/fast-export: plug leaking tag namesPatrick Steinhardt, Aug 14, 2024
  138. 14/22 merge-ort: unconditionally release attributes indexPatrick Steinhardt, Aug 14, 2024
  139. 15/22 sequencer: release todo list on error pathsPatrick Steinhardt, Aug 14, 2024
  140. 16/22 unpack-trees: clear index when not propagating itPatrick Steinhardt, Aug 14, 2024
  141. 17/22 diff: fix leak when parsing invalid ignore regex optionPatrick Steinhardt, Aug 14, 2024
  142. 18/22 builtin/format-patch: fix various trivial memory leaksPatrick Steinhardt, Aug 14, 2024
  143. 19/22 userdiff: fix leaking memory for configured diff driversPatrick Steinhardt, Aug 14, 2024
  144. 20/22 builtin/log: fix leak when showing converted blob contentsPatrick Steinhardt, Aug 14, 2024
  145. 21/22 diff: free state populated via optionsPatrick Steinhardt, Aug 14, 2024
  146. 22/22 builtin/diff: free symmetric diff membersPatrick Steinhardt, Aug 14, 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.