builtin/submodule--helper: clear child process when not running it

In `runcommand_in_submodule_cb()` we may end up not executing the child command when `argv` is empty. But we still populate the command with environment variables and other things, which needs cleanup. This leads to a memory leak because we do not call `finish_command()`. Fix this by clearing the child process when we don't execute it. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 26, 2024 at 13:46 UTC 8f786a8e9fbbc8d110937a0ffdfe559f945aab50
2 files changed +8 -3
builtin/submodule--helper.c
+7 -3
@@ -363,9 +363,13 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
363 if (!info->quiet)
364 printf(_("Entering '%s'\n"), displaypath);
365
366 - if (info->argv[0] && run_command(&cp))
367 - die(_("run_command returned non-zero status for %s\n."),
368 - displaypath);
366 + if (info->argv[0]) {
367 + if (run_command(&cp))
368 + die(_("run_command returned non-zero status for %s\n."),
369 + displaypath);
370 + } else {
371 + child_process_clear(&cp);
372 + }
373
374 if (info->recursive) {
375 struct child_process cpr = CHILD_PROCESS_INIT;
t/t7407-submodule-foreach.sh
+1
@@ -12,6 +12,7 @@ that are currently checked out.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 +TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
17
18