am: drop "dir" parameter from am_state_init

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 20, 2017 at 17:09 UTC 16d2676c9ee996208277772fdf81dda212355440
1 file changed +4 -6
builtin/am.c
+4 -6
@@ -134,17 +134,15 @@ struct am_state {
134 };
135
136 /**
137 - * Initializes am_state with the default values. The state directory is set to
138 - * dir.
137 + * Initializes am_state with the default values.
138 */
140 -static void am_state_init(struct am_state *state, const char *dir)
139 +static void am_state_init(struct am_state *state)
140 {
141 int gpgsign;
142
143 memset(state, 0, sizeof(*state));
144
146 - assert(dir);
147 - state->dir = xstrdup(dir);
145 + state->dir = git_pathdup("rebase-apply");
146
147 state->prec = 4;
148
@@ -2322,7 +2320,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2320
2321 git_config(git_am_config, NULL);
2322
2325 - am_state_init(&state, git_path("rebase-apply"));
2323 + am_state_init(&state);
2324
2325 in_progress = am_in_progress(&state);
2326 if (in_progress)