Skip to content

Commit ec007cd

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f6ba781 commit ec007cd

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

ref-filter.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static struct used_atom {
233233
enum { S_BARE, S_GRADE, S_SIGNER, S_KEY,
234234
S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option;
235235
} signature;
236-
const char **describe_args;
236+
struct strvec describe_args;
237237
struct refname_atom refname;
238238
char *head;
239239
} u;
@@ -693,7 +693,7 @@ static int describe_atom_parser(struct ref_format *format UNUSED,
693693
struct used_atom *atom,
694694
const char *arg, struct strbuf *err)
695695
{
696-
struct strvec args = STRVEC_INIT;
696+
strvec_init(&atom->u.describe_args);
697697

698698
for (;;) {
699699
int found = 0;
@@ -702,13 +702,12 @@ static int describe_atom_parser(struct ref_format *format UNUSED,
702702
if (!arg || !*arg)
703703
break;
704704

705-
found = describe_atom_option_parser(&args, &arg, err);
705+
found = describe_atom_option_parser(&atom->u.describe_args, &arg, err);
706706
if (found < 0)
707707
return found;
708708
if (!found)
709709
return err_bad_arg(err, "describe", bad_arg);
710710
}
711-
atom->u.describe_args = strvec_detach(&args);
712711
return 0;
713712
}
714713

@@ -1941,7 +1940,7 @@ static void grab_describe_values(struct atom_value *val, int deref,
19411940

19421941
cmd.git_cmd = 1;
19431942
strvec_push(&cmd.args, "describe");
1944-
strvec_pushv(&cmd.args, atom->u.describe_args);
1943+
strvec_pushv(&cmd.args, atom->u.describe_args.v);
19451944
strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
19461945
if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) {
19471946
error(_("failed to run 'describe'"));
@@ -3004,6 +3003,8 @@ void ref_array_clear(struct ref_array *array)
30043003
struct used_atom *atom = &used_atom[i];
30053004
if (atom->atom_type == ATOM_HEAD)
30063005
free(atom->u.head);
3006+
else if (atom->atom_type == ATOM_DESCRIBE)
3007+
strvec_clear(&atom->u.describe_args);
30073008
else if (atom->atom_type == ATOM_TRAILERS ||
30083009
(atom->atom_type == ATOM_CONTENTS &&
30093010
atom->u.contents.option == C_TRAILERS)) {

0 commit comments

Comments
 (0)