gpg-interface: fix leak of "line" in parse_ssh_output()
We xmemdupz() this buffer, but never free it. Let's do so. We'll use a cleanup label, since there are multiple exits from the function. Note that it was also declared a "const char *". We could switch that to "char *" to indicate that it's allocated, but that make it awkward to use with skip_prefix(). So instead, we'll introduce an extra non-const pointer. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Oct 18, 2021 at 13:15 UTC
78d468f1a9c7bf9d1724840ff322b9144061b308
1 file changed
+6
-2
gpg-interface.c
+6
-2
@@ -365,6 +365,7 @@ static int verify_gpg_signed_buffer(struct signature_check *sigc,
365
static void parse_ssh_output(struct signature_check *sigc)
366
{
367
const char *line, *principal, *search;
368
+ char *to_free;
369
char *key = NULL;
370
371
/*
@@ -383,7 +384,7 @@ static void parse_ssh_output(struct signature_check *sigc)
384
sigc->result = 'B';
385
sigc->trust_level = TRUST_NEVER;
386
386
- line = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
387
+ line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
388
389
if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
390
/* Valid signature and known principal */
@@ -403,7 +404,7 @@ static void parse_ssh_output(struct signature_check *sigc)
404
sigc->result = 'G';
405
sigc->trust_level = TRUST_UNDEFINED;
406
} else {
406
- return;
407
+ goto cleanup;
408
}
409
410
key = strstr(line, "key");
@@ -417,6 +418,9 @@ static void parse_ssh_output(struct signature_check *sigc)
418
*/
419
sigc->result = 'B';
420
}
421
+
422
+cleanup:
423
+ free(to_free);
424
}
425
426
static int verify_ssh_signed_buffer(struct signature_check *sigc,