Files
git/builtin/annotate.c
Patrick Steinhardt 9a48fc1da2 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>
2024-09-30 11:23:02 -07:00

38 lines
795 B
C

/*
* "git annotate" builtin alias
*
* Copyright (C) 2006 Ryan Anderson
*/
#define USE_THE_REPOSITORY_VARIABLE
#include "git-compat-util.h"
#include "builtin.h"
#include "strvec.h"
int cmd_annotate(int argc,
const char **argv,
const char *prefix,
struct repository *repo UNUSED)
{
struct strvec args = STRVEC_INIT;
const char **args_copy;
int ret;
strvec_pushl(&args, "annotate", "-c", NULL);
for (int i = 1; i < argc; i++)
strvec_push(&args, argv[i]);
/*
* `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;
}