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>

Patrick Steinhardt committed Nov 5, 2024 at 07:17 UTC 3f692fe5beb817fbb22754281dfb5ebf8863d0a3
1 file changed +8 -2
trailer.c
+8 -2
@@ -249,7 +249,9 @@ static char *apply_command(struct conf_info *conf, const char *arg)
249 static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
250 {
251 if (arg_tok->conf.command || arg_tok->conf.cmd) {
252 - const char *arg;
252 + char *value_to_free = NULL;
253 + char *arg;
254 +
255 if (arg_tok->value && arg_tok->value[0]) {
256 arg = arg_tok->value;
257 } else {
@@ -257,9 +259,13 @@ static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg
259 arg = xstrdup(in_tok->value);
260 else
261 arg = xstrdup("");
262 + value_to_free = arg_tok->value;
263 }
264 +
265 arg_tok->value = apply_command(&arg_tok->conf, arg);
262 - free((char *)arg);
266 +
267 + free(value_to_free);
268 + free(arg);
269 }
270 }
271