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

[PATCH 6/6] am: drop "dir" parameter from am_state_init

From
Jeff King <peff@peff.net>
Date
Apr 20, 2017, 21:09 UTC
Message-ID
<20170420210935.dx2old5l6yuakqhq@sigill.intra.peff.net>
In-Reply-To
<20170420210754.bzrnc74dpjs7fgpf@sigill.intra.peff.net>

The only caller of this function passes in a static buffer returned from git_path(). This looks dangerous at first glance, but turns out to be OK because the first thing we do is xstrdup() the result.

Let's turn this into a git_pathdup(). That's slightly more efficient (no extra copy), and makes it easier to audit for dangerous git_path() invocations.

Since there's only a single caller, let's just set this default path inside the init function. That makes the memory ownership clear.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/am.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index f7a7a971f..97849d4dc 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -134,17 +134,15 @@ struct am_state {
 };
 
 /**
- * Initializes am_state with the default values. The state directory is set to
- * dir.
+ * Initializes am_state with the default values.
  */
-static void am_state_init(struct am_state *state, const char *dir)
+static void am_state_init(struct am_state *state)
 {
 	int gpgsign;
 
 	memset(state, 0, sizeof(*state));
 
-	assert(dir);
-	state->dir = xstrdup(dir);
+	state->dir = git_pathdup("rebase-apply");
 
 	state->prec = 4;
 
@@ -2322,7 +2320,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
 
 	git_config(git_am_config, NULL);
 
-	am_state_init(&state, git_path("rebase-apply"));
+	am_state_init(&state);
 
 	in_progress = am_in_progress(&state);
 	if (in_progress)
-- 
2.13.0.rc0.363.g8726c260e
Previous: Jeff KingNext: Junio C Hamano
Message 7 of 8 in “removing more calls to git_path()”
  1. 0/6 removing more calls to git_path()Jeff King, Apr 20, 2017
  2. 1/6 bisect: add git_path_bisect_terms helperJeff King, Apr 20, 2017
  3. 2/6 branch: add edit_description() helperJeff King, Apr 20, 2017
  4. 3/6 use git_path_* helper functionsJeff King, Apr 20, 2017
  5. 4/6 replace xstrdup(git_path(...)) with git_pathdup(...)Jeff King, Apr 20, 2017
  6. 5/6 replace strbuf_addstr(git_path()) with git_path_buf()Jeff King, Apr 20, 2017
  7. 6/6 am: drop "dir" parameter from am_state_initJeff King, Apr 20, 2017
  8. Junio C HamanoApr 21, 2017

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.