Skip to content

Commit 9a48fc1

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a503122 commit 9a48fc1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

builtin/annotate.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ int cmd_annotate(int argc,
1515
struct repository *repo UNUSED)
1616
{
1717
struct strvec args = STRVEC_INIT;
18-
int i;
18+
const char **args_copy;
19+
int ret;
1920

2021
strvec_pushl(&args, "annotate", "-c", NULL);
21-
22-
for (i = 1; i < argc; i++) {
22+
for (int i = 1; i < argc; i++)
2323
strvec_push(&args, argv[i]);
24-
}
2524

26-
return cmd_blame(args.nr, args.v, prefix, the_repository);
25+
/*
26+
* `cmd_blame()` ends up modifying the array, which causes memory leaks
27+
* if we didn't copy the array here.
28+
*/
29+
CALLOC_ARRAY(args_copy, args.nr + 1);
30+
COPY_ARRAY(args_copy, args.v, args.nr);
31+
32+
ret = cmd_blame(args.nr, args_copy, prefix, the_repository);
33+
34+
strvec_clear(&args);
35+
free(args_copy);
36+
return ret;
2737
}

t/t8001-annotate.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

77
TEST_CREATE_REPO_NO_TEMPLATE=1
8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
PROG='git annotate'

0 commit comments

Comments
 (0)