g10: Don't leak memory if we fail to initialize a new database handle.

* g10/keydb.c (keydb_new): If we fail to open a keyring or keybox
correctly release all resources.

--
Signed-off-by: Neal H. Walfield <neal@g10code.com>.
This commit is contained in:
Neal H. Walfield 2015-08-31 11:22:14 +02:00
parent 360b699e9b
commit 04a6b903d0
1 changed files with 12 additions and 9 deletions

View File

@ -692,6 +692,7 @@ keydb_new (void)
{
KEYDB_HANDLE hd;
int i, j;
int die = 0;
if (DBG_CLOCK)
log_clock ("keydb_new");
@ -702,7 +703,7 @@ keydb_new (void)
hd->is_reset = 1;
assert (used_resources <= MAX_KEYDB_RESOURCES);
for (i=j=0; i < used_resources; i++)
for (i=j=0; ! die && i < used_resources; i++)
{
switch (all_resources[i].type)
{
@ -712,10 +713,8 @@ keydb_new (void)
hd->active[j].type = all_resources[i].type;
hd->active[j].token = all_resources[i].token;
hd->active[j].u.kr = keyring_new (all_resources[i].token);
if (!hd->active[j].u.kr) {
xfree (hd);
return NULL; /* fixme: release all previously allocated handles*/
}
if (!hd->active[j].u.kr)
die = 1;
j++;
break;
case KEYDB_RESOURCE_TYPE_KEYBOX:
@ -723,10 +722,7 @@ keydb_new (void)
hd->active[j].token = all_resources[i].token;
hd->active[j].u.kb = keybox_new_openpgp (all_resources[i].token, 0);
if (!hd->active[j].u.kb)
{
xfree (hd);
return NULL; /* fixme: release all previously allocated handles*/
}
die = 1;
j++;
break;
}
@ -734,6 +730,13 @@ keydb_new (void)
hd->used = j;
active_handles++;
if (die)
{
keydb_release (hd);
hd = NULL;
}
return hd;
}