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>

Patrick Steinhardt committed Aug 1, 2024 at 12:39 UTC 6771e2012e15ed3748997566640c34b492c2cf67
2 files changed +8 -2
builtin/submodule--helper.c
+7 -2
@@ -2268,6 +2268,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
2268 struct child_process cp = CHILD_PROCESS_INIT;
2269 struct strbuf rev = STRBUF_INIT;
2270 char *hex = oid_to_hex(oid);
2271 + int reachable;
2272
2273 cp.git_cmd = 1;
2274 cp.dir = path;
@@ -2277,9 +2278,12 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
2278 prepare_submodule_repo_env(&cp.env);
2279
2280 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2280 - return 0;
2281 + reachable = 0;
2282 + else
2283 + reachable = 1;
2284
2282 - return 1;
2285 + strbuf_release(&rev);
2286 + return reachable;
2287 }
2288
2289 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)
3226 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3227 }
3228 ret = 0;
3229 +
3230 cleanup:
3231 string_list_clear(&reference, 1);
3232 return ret;
t/t7400-submodule-basic.sh
+1
@@ -12,6 +12,7 @@ subcommands of git submodule.
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 test_expect_success 'setup - enable local submodules' '