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

[PATCH v2 6/7] config: fix leak in git_config_get_expiry_in_days()

From
Jeff King <peff@peff.net>
Date
Aug 17, 2020, 21:33 UTC
Message-ID
<20200817213313.GF1854722@coredump.intra.peff.net>
In-Reply-To
<20200817213228.GA1854603@coredump.intra.peff.net>

We use git_config_get_string() to retrieve the expiry value in a newly allocated string. But after parsing it, we never free it, leaking the memory.

We could fix this with a free() obviously, but there's an even better solution: we can use the non-allocating "tmp" variant of the function; we only need it to be valid for the lifetime of our parse function.

Signed-off-by: Jeff King <peff@peff.net>
---
 config.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/config.c b/config.c
index f0367c76ad..2bdff4457b 100644
--- a/config.c
+++ b/config.c
@@ -2300,11 +2300,11 @@ int git_config_get_expiry(const char *key, const char **output)
 
 int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
 {
-	char *expiry_string;
+	const char *expiry_string;
 	intmax_t days;
 	timestamp_t when;
 
-	if (git_config_get_string(key, &expiry_string))
+	if (git_config_get_string_tmp(key, &expiry_string))
 		return 1; /* no such thing */
 
 	if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
-- 
2.28.0.605.g35fde94f44
Previous: Jeff KingNext: Jeff King
Message 18 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.