summaryrefslogtreecommitdiff
path: root/builtin/annotate.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-09-30 11:13:10 +0200
committerJunio C Hamano <gitster@pobox.com>2024-09-30 11:23:02 -0700
commit9a48fc1da277f37b602f48e8bec22f4725ebf877 (patch)
tree807f4459b7a7588952096beaff73adb573286997 /builtin/annotate.c
parenta5031223cda0f37f70b901f7a2b7cd78e0627d82 (diff)
builtin/annotate: fix leaking args vector
We're leaking the args vector in git-annotate(1) because we never clear it. Fixing it isn't as easy as calling `strvec_clear()` though because calling `cmd_blame()` will cause the underlying array to be modified. Instead, we also need to pass a shallow copy of the argv array to the function. Do so to plug the memory leaks. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/annotate.c')
-rw-r--r--builtin/annotate.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/builtin/annotate.c b/builtin/annotate.c
index a99179fe4d..03413c7df8 100644
--- a/builtin/annotate.c
+++ b/builtin/annotate.c
@@ -15,13 +15,23 @@ int cmd_annotate(int argc,
struct repository *repo UNUSED)
{
struct strvec args = STRVEC_INIT;
- int i;
+ const char **args_copy;
+ int ret;
strvec_pushl(&args, "annotate", "-c", NULL);
-
- for (i = 1; i < argc; i++) {
+ for (int i = 1; i < argc; i++)
strvec_push(&args, argv[i]);
- }
- return cmd_blame(args.nr, args.v, prefix, the_repository);
+ /*
+ * `cmd_blame()` ends up modifying the array, which causes memory leaks
+ * if we didn't copy the array here.
+ */
+ CALLOC_ARRAY(args_copy, args.nr + 1);
+ COPY_ARRAY(args_copy, args.v, args.nr);
+
+ ret = cmd_blame(args.nr, args_copy, prefix, the_repository);
+
+ strvec_clear(&args);
+ free(args_copy);
+ return ret;
}