ref-filter: fix leak with %(describe) arguments

When we parse a %(describe) placeholder, we stuff its arguments into a strvec, which is then detached into the used_atom struct. But later, when ref_array_clear() frees the atom, we never free the memory. To solve this, we just need to add the appropriate free() calls. But it's a little awkward, since we have to free each element of the array, in addition to the array itself. Instead, let's store the actual strvec, which lets us do a simple strvec_clear(). This clears up one case that LSan finds in t6300, but there are more. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 9, 2024 at 19:19 UTC ec007cde941226ed35434ea6443ad1ba066279cf
1 file changed +6 -5
ref-filter.c
+6 -5
@@ -233,7 +233,7 @@ static struct used_atom {
233 enum { S_BARE, S_GRADE, S_SIGNER, S_KEY,
234 S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option;
235 } signature;
236 - const char **describe_args;
236 + struct strvec describe_args;
237 struct refname_atom refname;
238 char *head;
239 } u;
@@ -693,7 +693,7 @@ static int describe_atom_parser(struct ref_format *format UNUSED,
693 struct used_atom *atom,
694 const char *arg, struct strbuf *err)
695 {
696 - struct strvec args = STRVEC_INIT;
696 + strvec_init(&atom->u.describe_args);
697
698 for (;;) {
699 int found = 0;
@@ -702,13 +702,12 @@ static int describe_atom_parser(struct ref_format *format UNUSED,
702 if (!arg || !*arg)
703 break;
704
705 - found = describe_atom_option_parser(&args, &arg, err);
705 + found = describe_atom_option_parser(&atom->u.describe_args, &arg, err);
706 if (found < 0)
707 return found;
708 if (!found)
709 return err_bad_arg(err, "describe", bad_arg);
710 }
711 - atom->u.describe_args = strvec_detach(&args);
711 return 0;
712 }
713
@@ -1941,7 +1940,7 @@ static void grab_describe_values(struct atom_value *val, int deref,
1940
1941 cmd.git_cmd = 1;
1942 strvec_push(&cmd.args, "describe");
1944 - strvec_pushv(&cmd.args, atom->u.describe_args);
1943 + strvec_pushv(&cmd.args, atom->u.describe_args.v);
1944 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1945 if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) {
1946 error(_("failed to run 'describe'"));
@@ -3004,6 +3003,8 @@ void ref_array_clear(struct ref_array *array)
3003 struct used_atom *atom = &used_atom[i];
3004 if (atom->atom_type == ATOM_HEAD)
3005 free(atom->u.head);
3006 + else if (atom->atom_type == ATOM_DESCRIBE)
3007 + strvec_clear(&atom->u.describe_args);
3008 else if (atom->atom_type == ATOM_TRAILERS ||
3009 (atom->atom_type == ATOM_CONTENTS &&
3010 atom->u.contents.option == C_TRAILERS)) {