Skip to content

Commit 4e83605

Browse files
avargitster
authored andcommitted
submodule--helper: fix obscure leak in module_add()
Fix an obscure leak in module_add(), if the "git add" command we were piping to failed we'd fail to strbuf_release(&sb). This fixes a leak introduced in a6226fd (submodule--helper: convert the bulk of cmd_add() to C, 2021-08-10). In fixing it move to a "goto cleanup" pattern, and since we need to introduce a "ret" variable to do that let's also get rid of the intermediate "exit_code" variable. The initialization to "-1" in a6226fd has always been redundant, we'd only use the "exit_code" value after assigning the return value of pipe_command() to it. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Reviewed-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c81ee9 commit 4e83605

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

builtin/submodule--helper.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,6 +3293,8 @@ static int module_add(int argc, const char **argv, const char *prefix)
32933293
N_("git submodule add [<options>] [--] <repository> [<path>]"),
32943294
NULL
32953295
};
3296+
struct strbuf sb = STRBUF_INIT;
3297+
int ret = 1;
32963298

32973299
argc = parse_options(argc, argv, prefix, options, usage, 0);
32983300

@@ -3342,21 +3344,17 @@ static int module_add(int argc, const char **argv, const char *prefix)
33423344
die_on_repo_without_commits(add_data.sm_path);
33433345

33443346
if (!force) {
3345-
int exit_code = -1;
3346-
struct strbuf sb = STRBUF_INIT;
33473347
struct child_process cp = CHILD_PROCESS_INIT;
33483348

33493349
cp.git_cmd = 1;
33503350
cp.no_stdout = 1;
33513351
strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
33523352
"--no-warn-embedded-repo", add_data.sm_path, NULL);
3353-
if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3353+
if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
33543354
strbuf_complete_line(&sb);
33553355
fputs(sb.buf, stderr);
3356-
free(add_data.sm_path);
3357-
return exit_code;
3356+
goto cleanup;
33583357
}
3359-
strbuf_release(&sb);
33603358
}
33613359

33623360
if(!add_data.sm_name)
@@ -3371,15 +3369,17 @@ static int module_add(int argc, const char **argv, const char *prefix)
33713369
add_data.progress = !!progress;
33723370
add_data.dissociate = !!dissociate;
33733371

3374-
if (add_submodule(&add_data)) {
3375-
free(add_data.sm_path);
3376-
return 1;
3377-
}
3372+
if (add_submodule(&add_data))
3373+
goto cleanup;
33783374
configure_added_submodule(&add_data);
3375+
3376+
ret = 0;
3377+
cleanup:
33793378
free(add_data.sm_path);
33803379
free(to_free);
3380+
strbuf_release(&sb);
33813381

3382-
return 0;
3382+
return ret;
33833383
}
33843384

33853385
#define SUPPORT_SUPER_PREFIX (1<<0)

0 commit comments

Comments
 (0)