g10: Avoid memory leaks

* g10/call-agent.c (card_keyinfo_cb): free keyinfo.  Restructure to
avoid backward gotos.
* g10/keyedit.c (menu_set_keyserver_url): properly enclose the block
* g10/keygen.c (gen_card_key): free pk and pkt

--

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
GnuPG-bug-id: 5393

Additional changes:
- Restructure to avoid backward gotos.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Jakub Jelen 2021-04-12 20:05:48 +02:00 committed by Werner Koch
parent 25aa353bf8
commit fa0771f609
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 30 additions and 18 deletions

View File

@ -1709,6 +1709,7 @@ card_keyinfo_cb (void *opaque, const char *line)
struct card_keyinfo_parm_s *parm = opaque; struct card_keyinfo_parm_s *parm = opaque;
const char *keyword = line; const char *keyword = line;
int keywordlen; int keywordlen;
keypair_info_t keyinfo = NULL;
for (keywordlen=0; *line && !spacep (line); line++, keywordlen++) for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
; ;
@ -1719,7 +1720,6 @@ card_keyinfo_cb (void *opaque, const char *line)
{ {
const char *s; const char *s;
int n; int n;
keypair_info_t keyinfo;
keypair_info_t *l_p = &parm->list; keypair_info_t *l_p = &parm->list;
while ((*l_p)) while ((*l_p))
@ -1727,23 +1727,13 @@ card_keyinfo_cb (void *opaque, const char *line)
keyinfo = xtrycalloc (1, sizeof *keyinfo); keyinfo = xtrycalloc (1, sizeof *keyinfo);
if (!keyinfo) if (!keyinfo)
{ goto alloc_error;
alloc_error:
if (!parm->error)
parm->error = gpg_error_from_syserror ();
return 0;
}
for (n=0,s=line; hexdigitp (s); s++, n++) for (n=0,s=line; hexdigitp (s); s++, n++)
; ;
if (n != 40) if (n != 40)
{ goto parm_error;
parm_error:
if (!parm->error)
parm->error = gpg_error (GPG_ERR_ASS_PARAMETER);
return 0;
}
memcpy (keyinfo->keygrip, line, 40); memcpy (keyinfo->keygrip, line, 40);
keyinfo->keygrip[40] = 0; keyinfo->keygrip[40] = 0;
@ -1797,6 +1787,18 @@ card_keyinfo_cb (void *opaque, const char *line)
} }
return err; return err;
alloc_error:
xfree (keyinfo);
if (!parm->error)
parm->error = gpg_error_from_syserror ();
return 0;
parm_error:
xfree (keyinfo);
if (!parm->error)
parm->error = gpg_error (GPG_ERR_ASS_PARAMETER);
return 0;
} }

View File

@ -5382,14 +5382,16 @@ menu_set_keyserver_url (ctrl_t ctrl, const char *url, kbnode_t pub_keyblock)
uri uri
? _("Are you sure you want to replace it? (y/N) ") ? _("Are you sure you want to replace it? (y/N) ")
: _("Are you sure you want to delete it? (y/N) "))) : _("Are you sure you want to delete it? (y/N) ")))
xfree (user); {
continue; xfree (user);
continue;
}
} }
else if (uri == NULL) else if (uri == NULL)
{ {
/* There is no current keyserver URL, so there /* There is no current keyserver URL, so there
is no point in trying to un-set it. */ is no point in trying to un-set it. */
xfree (user); xfree (user);
continue; continue;
} }

View File

@ -6140,12 +6140,20 @@ gen_card_key (int keyno, int algo, int is_primary, kbnode_t pub_root,
the self-signatures. */ the self-signatures. */
err = agent_readkey (NULL, 1, keyid, &public); err = agent_readkey (NULL, 1, keyid, &public);
if (err) if (err)
return err; {
xfree (pkt);
xfree (pk);
return err;
}
err = gcry_sexp_sscan (&s_key, NULL, public, err = gcry_sexp_sscan (&s_key, NULL, public,
gcry_sexp_canon_len (public, 0, NULL, NULL)); gcry_sexp_canon_len (public, 0, NULL, NULL));
xfree (public); xfree (public);
if (err) if (err)
return err; {
xfree (pkt);
xfree (pk);
return err;
}
if (algo == PUBKEY_ALGO_RSA) if (algo == PUBKEY_ALGO_RSA)
err = key_from_sexp (pk->pkey, s_key, "public-key", "ne"); err = key_from_sexp (pk->pkey, s_key, "public-key", "ne");