trailer: fix leaking trailer values

Fix leaking trailer values when replacing the value with a command or
when the token value is empty.

This leak is exposed by t7513, but plugging it does not make the whole
test suite pass.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-11-05 07:17:12 +01:00
committed by Junio C Hamano
parent d34b5cbf02
commit 3f692fe5be

View File

@@ -249,7 +249,9 @@ static char *apply_command(struct conf_info *conf, const char *arg)
static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
{ {
if (arg_tok->conf.command || arg_tok->conf.cmd) { if (arg_tok->conf.command || arg_tok->conf.cmd) {
const char *arg; char *value_to_free = NULL;
char *arg;
if (arg_tok->value && arg_tok->value[0]) { if (arg_tok->value && arg_tok->value[0]) {
arg = arg_tok->value; arg = arg_tok->value;
} else { } else {
@@ -257,9 +259,13 @@ static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg
arg = xstrdup(in_tok->value); arg = xstrdup(in_tok->value);
else else
arg = xstrdup(""); arg = xstrdup("");
value_to_free = arg_tok->value;
} }
arg_tok->value = apply_command(&arg_tok->conf, arg); arg_tok->value = apply_command(&arg_tok->conf, arg);
free((char *)arg);
free(value_to_free);
free(arg);
} }
} }