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

[PATCH 1/3] format_config: don't init strbuf

From
Jeff King <peff@peff.net>
Date
Aug 20, 2015, 14:46 UTC
Message-ID
<20150820144604.GA11913@sigill.intra.peff.net>
In-Reply-To
<20150820144504.GA22935@sigill.intra.peff.net>

It's unusual for a function which writes to a passed-in strbuf to call strbuf_init; that will throw away anything already there, leaking memory. In this case, there are exactly two callers; one relies on this initialization and the other passes in an already-initialized buffer.

There's no leak, as the initialized buffer doesn't have anything in it. But let's bump the strbuf_init out to the one caller who needs it, making format_config more idiomatic.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/config.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 810e104..91aa56f 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -108,8 +108,6 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
-	strbuf_init(buf, 0);
-
 	if (show_keys)
 		strbuf_addstr(buf, key_);
 	if (!omit_values) {
@@ -166,6 +164,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
 		return 0;
 
 	ALLOC_GROW(values->items, values->nr + 1, values->alloc);
+	strbuf_init(&values->items[values->nr], 0);
 
 	return format_config(&values->items[values->nr++], key_, value_);
 }
-- 
2.5.0.680.g69e7703
Previous: Jeff KingNext: Jeff King
Message 9 of 18 in “[PATCHv3 0/2] 'git config --names-only' to help the completion script”
  1. SZEDER GáborAug 10, 2015
  2. 1/2 config: add '--names-only' option to list only variable namesSZEDER Gábor, Aug 10, 2015
  3. Jeff KingAug 10, 2015
  4. Junio C HamanoAug 10, 2015
  5. SZEDER GáborAug 12, 2015
  6. Junio C HamanoAug 13, 2015
  7. config: restructure format_config() for better control flowSZEDER Gábor, Aug 20, 2015
  8. Jeff KingAug 20, 2015
  9. 1/3 format_config: don't init strbufJeff King, Aug 20, 2015
  10. 2/3 format_config: simplify buffer handlingJeff King, Aug 20, 2015
  11. SZEDER GáborAug 21, 2015
  12. Junio C HamanoAug 21, 2015
  13. Junio C HamanoAug 21, 2015
  14. Jeff KingAug 21, 2015
  15. 3/3 get_urlmatch: avoid useless strbuf writeJeff King, Aug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. 2/2 completion: list variable names reliably with 'git config --names-only'SZEDER Gábor, Aug 10, 2015
  18. Jeff KingAug 10, 2015

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.