submodule--helper: don't leak {run,capture}_command() cp.dir argument

Fix a memory leak in c51f8f94e5b (submodule--helper: run update procedures from C, 2021-08-24) and 3c3558f0953 (submodule--helper: run update using child process struct, 2022-03-15) by not allocating memory in the first place. The "dir" member of "struct child_process" will not be modified by that API, and it's declared to be "const char *". So let's not needlessly duplicate these strings. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 1, 2022 at 01:14 UTC 0a4d31537d66bd2249adc8d923e2e6829204059e
1 file changed +3 -3
builtin/submodule--helper.c
+3 -3
@@ -2126,7 +2126,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
2126 char *hex = oid_to_hex(oid);
2127
2128 cp.git_cmd = 1;
2129 - cp.dir = xstrdup(path);
2129 + cp.dir = path;
2130 cp.no_stderr = 1;
2131 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2132
@@ -2145,7 +2145,7 @@ static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2145
2146 prepare_submodule_repo_env(&cp.env);
2147 cp.git_cmd = 1;
2148 - cp.dir = xstrdup(module_path);
2148 + cp.dir = module_path;
2149
2150 strvec_push(&cp.args, "fetch");
2151 if (quiet)
@@ -2198,7 +2198,7 @@ static int run_update_command(const struct update_data *ud, int subforce)
2198 }
2199 strvec_push(&cp.args, oid);
2200
2201 - cp.dir = xstrdup(ud->sm_path);
2201 + cp.dir = ud->sm_path;
2202 prepare_submodule_repo_env(&cp.env);
2203 if ((ret = run_command(&cp))) {
2204 switch (ud->update_strategy.type) {