builtin/describe: fix leaking array when running diff-index

When running git-describe(1) with `--dirty`, we will set up a `struct rev_info` with arguments for git-diff-index(1). The way we assemble the arguments it causes two memory leaks though: - We never release the `struct strvec`. - `setup_revisions()` may end up removing some entries from the `strvec`, which we wouldn't free even if we released the struct. While we could plug those leaks, this is ultimately unnecessary as the arguments we pass are part of a static array anyway. So instead, refactor the code to drop the `struct strvec` and just pass this static array directly. 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:38 UTC 5a1e1e5d4055e142369a05a6d71e5101eead8ab2
1 file changed +3 -3
builtin/describe.c
+3 -3
@@ -695,7 +695,6 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
695 } else if (dirty) {
696 struct lock_file index_lock = LOCK_INIT;
697 struct rev_info revs;
698 - struct strvec args = STRVEC_INIT;
698 int fd;
699
700 setup_work_tree();
@@ -710,8 +709,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
709 repo_update_index_if_able(the_repository, &index_lock);
710
711 repo_init_revisions(the_repository, &revs, prefix);
713 - strvec_pushv(&args, diff_index_args);
714 - if (setup_revisions(args.nr, args.v, &revs, NULL) != 1)
712 +
713 + if (setup_revisions(ARRAY_SIZE(diff_index_args) - 1,
714 + diff_index_args, &revs, NULL) != 1)
715 BUG("malformed internal diff-index command line");
716 run_diff_index(&revs, 0);
717