Skip to content

Commit 78d468f

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

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

gpg-interface.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ static int verify_gpg_signed_buffer(struct signature_check *sigc,
365365
static void parse_ssh_output(struct signature_check *sigc)
366366
{
367367
const char *line, *principal, *search;
368+
char *to_free;
368369
char *key = NULL;
369370

370371
/*
@@ -383,7 +384,7 @@ static void parse_ssh_output(struct signature_check *sigc)
383384
sigc->result = 'B';
384385
sigc->trust_level = TRUST_NEVER;
385386

386-
line = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
387+
line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
387388

388389
if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
389390
/* Valid signature and known principal */
@@ -403,7 +404,7 @@ static void parse_ssh_output(struct signature_check *sigc)
403404
sigc->result = 'G';
404405
sigc->trust_level = TRUST_UNDEFINED;
405406
} else {
406-
return;
407+
goto cleanup;
407408
}
408409

409410
key = strstr(line, "key");
@@ -417,6 +418,9 @@ static void parse_ssh_output(struct signature_check *sigc)
417418
*/
418419
sigc->result = 'B';
419420
}
421+
422+
cleanup:
423+
free(to_free);
420424
}
421425

422426
static int verify_ssh_signed_buffer(struct signature_check *sigc,

0 commit comments

Comments
 (0)