submodule--helper: fix a memory leak in "status"

The "status" sub-command was leaking the "struct strvec" it was
setting up for the reasons explained in f92dbdbc6a (revisions API:
don't leak memory on argv elements that need free()-ing, 2022-08-02),
so let's use the "free_removed_argv_elements" option to
setup_revisions() to fix the leak.

Even if we did that, clobbering the "diff_files_args.nr" with the
return value of setup_revisions() would leave leaks in place, but we
can just stop clobbering it.

Ever since that code was added in a9f8a37584 (submodule: port
submodule subcommand 'status' from shell to C, 2017-10-06) we've had
no reason to modify the "nr" member ("argc" at the time): The next use
of "diff_files_args" after this is the "strvec_clear()" at the end of
the function.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2022-11-08 15:10:34 +01:00 committed by Taylor Blau
parent 44874cbd19
commit 435285bd82
2 changed files with 5 additions and 3 deletions

View File

@ -616,6 +616,9 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
int diff_files_result; int diff_files_result;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
const char *git_dir; const char *git_dir;
struct setup_revision_opt opt = {
.free_removed_argv_elements = 1,
};
if (!submodule_from_path(the_repository, null_oid(), path)) if (!submodule_from_path(the_repository, null_oid(), path))
die(_("no submodule mapping found in .gitmodules for path '%s'"), die(_("no submodule mapping found in .gitmodules for path '%s'"),
@ -649,9 +652,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
repo_init_revisions(the_repository, &rev, NULL); repo_init_revisions(the_repository, &rev, NULL);
rev.abbrev = 0; rev.abbrev = 0;
diff_files_args.nr = setup_revisions(diff_files_args.nr, setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
diff_files_args.v,
&rev, NULL);
diff_files_result = run_diff_files(&rev, 0); diff_files_result = run_diff_files(&rev, 0);
if (!diff_result_code(&rev.diffopt, diff_files_result)) { if (!diff_result_code(&rev.diffopt, diff_files_result)) {

View File

@ -2,6 +2,7 @@
test_description='submodule --cached, --quiet etc. output' test_description='submodule --cached, --quiet etc. output'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY"/lib-t3100.sh . "$TEST_DIRECTORY"/lib-t3100.sh