builtin/submodule--helper: fix leaking buffer in is_tip_reachable

The `rev` buffer in `is_tip_reachable()` is being populated with the
output of git-rev-list(1) -- if either the command fails or the buffer
contains any data, then the input commit is not reachable.

The buffer isn't used for anything else, but neither do we free it,
causing a memory leak. Fix this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-08-01 12:39:57 +02:00
committed by Junio C Hamano
parent 5535b3f3d3
commit 6771e2012e
2 changed files with 8 additions and 2 deletions

View File

@ -2268,6 +2268,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf rev = STRBUF_INIT;
char *hex = oid_to_hex(oid);
int reachable;
cp.git_cmd = 1;
cp.dir = path;
@ -2277,9 +2278,12 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
prepare_submodule_repo_env(&cp.env);
if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
return 0;
reachable = 0;
else
reachable = 1;
return 1;
strbuf_release(&rev);
return reachable;
}
static int fetch_in_submodule(const char *module_path, int depth, int quiet,
@ -3222,6 +3226,7 @@ static int add_submodule(const struct add_data *add_data)
die(_("unable to checkout submodule '%s'"), add_data->sm_path);
}
ret = 0;
cleanup:
string_list_clear(&reference, 1);
return ret;

View File

@ -12,6 +12,7 @@ subcommands of git submodule.
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup - enable local submodules' '