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

[PATCH 6/6] submodule--helper: fix leak of core.worktree value

From
Jeff King <peff@peff.net>
Date
Aug 14, 2020, 16:20 UTC
Message-ID
<20200814162035.GF595698@coredump.intra.peff.net>
In-Reply-To
<20200814161328.GA153929@coredump.intra.peff.net>

In the ensure_core_worktree() function, we load the core.worktree value of the submodule repository using repo_config_get_string(). This function copies the string, but we never free it, leaking the memory.

We can instead use the "tmp" version of that function to avoid the allocation at all. We don't have to worry about lifetime issues, since we never even look at the value (we just want to know if it's set).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/submodule--helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index e09605716e..a59d8e4bda 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2101,7 +2101,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
 {
 	const struct submodule *sub;
 	const char *path;
-	char *cw;
+	const char *cw;
 	struct repository subrepo;
 
 	if (argc != 2)
@@ -2116,7 +2116,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
 	if (repo_submodule_init(&subrepo, the_repository, sub))
 		die(_("could not get a repository handle for submodule '%s'"), path);
 
-	if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
+	if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
 		char *cfg_file, *abs_path;
 		const char *rel_path;
 		struct strbuf sb = STRBUF_INIT;
-- 
2.28.0.596.g9c08d63829
Previous: Jeff KingNext: Jeff King
Message 10 of 20 in “more small leak fixes”
  1. 0/6 more small leak fixesJeff King, Aug 14, 2020
  2. 1/6 submodule--helper: use strbuf_release() to free strbufsJeff King, Aug 14, 2020
  3. 2/6 checkout: fix leak of non-existent branch namesJeff King, Aug 14, 2020
  4. 3/6 config: fix leaks from git_config_get_string_const()Jeff King, Aug 14, 2020
  5. 4/6 config: drop git_config_get_string_const()Jeff King, Aug 14, 2020
  6. Junio C HamanoAug 14, 2020
  7. Jeff KingAug 15, 2020
  8. Junio C HamanoAug 17, 2020
  9. 5/6 config: fix leak in git_config_get_expiry_in_days()Jeff King, Aug 14, 2020
  10. 6/6 submodule--helper: fix leak of core.worktree valueJeff King, Aug 14, 2020
  11. Jeff KingAug 14, 2020
  12. Jeff KingAug 14, 2020
  13. 0/7 leak fixesJeff King, Aug 17, 2020
  14. 1/7 clear_pattern_list(): clear embedded hashmapsJeff King, Aug 17, 2020
  15. 2/7 submodule--helper: use strbuf_release() to free strbufsJeff King, Aug 17, 2020
  16. 3/7 checkout: fix leak of non-existent branch namesJeff King, Aug 17, 2020
  17. 4/7 config: fix leaks from git_config_get_string_const()Jeff King, Aug 17, 2020
  18. 6/7 config: fix leak in git_config_get_expiry_in_days()Jeff King, Aug 17, 2020
  19. 7/7 submodule--helper: fix leak of core.worktree valueJeff King, Aug 17, 2020
  20. 5/7 config: drop git_config_get_string_const()Jeff King, Aug 17, 2020

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.