]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/annotate: fix leaking args vector
authorPatrick Steinhardt <ps@pks.im>
Mon, 30 Sep 2024 09:13:10 +0000 (11:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Sep 2024 18:23:02 +0000 (11:23 -0700)
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>
builtin/annotate.c
t/t8001-annotate.sh

index a99179fe4dd9aea68574842abce093f483d20caa..03413c7df8d86c5b267e01a8eb6efb0f80d80b7c 100644 (file)
@@ -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;
 }
index d7167f55397f57f35a75eccff662c13b4f712ec8..d4346989195d53f10c30b1dadc815ffed1800c02 100755 (executable)
@@ -5,6 +5,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 PROG='git annotate'