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

[PATCH v3 6/6] refs: plug memory leak in repo_default_branch_name()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 22, 2021, 18:19 UTC
Message-ID
<patch-v3-6.6-d2f639b53cd-20211022T175227Z-avarab@gmail.com>
In-Reply-To
<cover-v3-0.6-00000000000-20211022T175227Z-avarab@gmail.com>

Fix a memory leak in repo_default_branch_name(), we'll leak memory before exit(128) here.

Normally we would not care much about such leaks, we do leak the memory, as e.g. valgrind(1) will report. But the more commonly used SANITIZE=leak mode will use GCC and Clang's LSAN mode will not normally report such leaks.

At least one GCC version does that in this case, and having the tests fail under -O3 would be annoying, so let's free() the allocated memory here.

This uses a new die_message() function introduced in a preceding commit. That new function makes the flow around such code easier to manage. In this case we can't free(ret) before the die().

In this case only the "free(full_ref)" appears to be needed, but since we're freeing one let's free both, some other compiler or version might arrange this code in such a way as to complain about "ret" too with SANITIZE=leak, and valgrind(1) will do so in any case.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 refs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/refs.c b/refs.c
index 7f019c2377e..2a816c9561d 100644
--- a/refs.c
+++ b/refs.c
@@ -577,6 +577,7 @@ char *repo_default_branch_name(struct repository *r, int quiet)
 	const char *config_display_key = "init.defaultBranch";
 	char *ret = NULL, *full_ref;
 	const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
+	int exit_with = 0;
 
 	if (env && *env)
 		ret = xstrdup(env);
@@ -591,8 +592,13 @@ char *repo_default_branch_name(struct repository *r, int quiet)
 
 	full_ref = xstrfmt("refs/heads/%s", ret);
 	if (check_refname_format(full_ref, 0))
-		die(_("invalid branch name: %s = %s"), config_display_key, ret);
+		exit_with = die_message(_("invalid branch name: %s = %s"),
+					config_display_key, ret);
 	free(full_ref);
+	if (exit_with) {
+		free(ret);
+		exit(exit_with);
+	}
 
 	return ret;
 }
-- 
2.33.1.1494.g88b39a443e1
Previous: Jeff KingNext: Jonathan Tan
Message 25 of 26 in “leak tests: free() before die for two API functions”
  1. leak tests: free() before die for two API functionsÆvar Arnfjörð Bjarmason, Oct 21, 2021
  2. Andrzej HuntOct 21, 2021
  3. Junio C HamanoOct 21, 2021
  4. Martin ÅgrenOct 21, 2021
  5. 0/3 refs.c + config.c: plug memory leaksÆvar Arnfjörð Bjarmason, Oct 21, 2021
  6. 2/3 config.c: don't leak memory in handle_path_include()Ævar Arnfjörð Bjarmason, Oct 21, 2021
  7. Junio C HamanoOct 21, 2021
  8. Ævar Arnfjörð BjarmasonOct 22, 2021
  9. Junio C HamanoOct 22, 2021
  10. Ævar Arnfjörð BjarmasonOct 22, 2021
  11. 1/3 refs.c: make "repo_default_branch_name" static, remove xstrfmt()Ævar Arnfjörð Bjarmason, Oct 21, 2021
  12. Junio C HamanoOct 21, 2021
  13. 3/3 config.c: free(expanded) before die(), work around GCC oddityÆvar Arnfjörð Bjarmason, Oct 21, 2021
  14. Junio C HamanoOct 21, 2021
  15. 0/6 usage.c: add die_message() & plug memory leaks in refs.c & config.cÆvar Arnfjörð Bjarmason, Oct 22, 2021
  16. 1/6 usage.c: add a die_message() routineÆvar Arnfjörð Bjarmason, Oct 22, 2021
  17. Junio C HamanoOct 24, 2021
  18. 2/6 usage.c API users: use die_message() where appropriateÆvar Arnfjörð Bjarmason, Oct 22, 2021
  19. 3/6 usage.c + gc: add and use a die_message_errno()Ævar Arnfjörð Bjarmason, Oct 22, 2021
  20. Junio C HamanoOct 24, 2021
  21. 4/6 config.c: don't leak memory in handle_path_include()Ævar Arnfjörð Bjarmason, Oct 22, 2021
  22. Junio C HamanoOct 24, 2021
  23. 5/6 config.c: free(expanded) before die(), work around GCC oddityÆvar Arnfjörð Bjarmason, Oct 22, 2021
  24. Jeff KingOct 26, 2021
  25. 6/6 refs: plug memory leak in repo_default_branch_name()Ævar Arnfjörð Bjarmason, Oct 22, 2021
  26. Jonathan TanOct 27, 2021

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.