1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-13 00:09:51 +02:00

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

View File

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