more paranoid on refcount, set refcounter ealier, reset pointers after free

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16472 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Love Hörnquist Åstrand
2006-01-04 14:55:50 +00:00
parent d4c217034c
commit 5952012ed9

View File

@@ -46,7 +46,7 @@ struct p11_module {
CK_FUNCTION_LIST_PTR funcs; CK_FUNCTION_LIST_PTR funcs;
CK_ULONG num_slots; CK_ULONG num_slots;
CK_ULONG selected_slot; CK_ULONG selected_slot;
int refcount; unsigned int refcount;
/* slot info */ /* slot info */
struct p11_slot { struct p11_slot {
int flags; int flags;
@@ -457,6 +457,8 @@ collect_private_key(struct p11_module *p, struct p11_slot *slot,
p11rsa->private_key = object; p11rsa->private_key = object;
p->refcount++; p->refcount++;
if (p->refcount == 0)
_hx509_abort("pkcs11 refcount to high");
RSA_set_method(rsa, &rsa_pkcs1_method); RSA_set_method(rsa, &rsa_pkcs1_method);
ret = RSA_set_app_data(rsa, p11rsa); ret = RSA_set_app_data(rsa, p11rsa);
@@ -580,6 +582,7 @@ p11_init(hx509_certs certs, void **data, int flags,
return ENOMEM; return ENOMEM;
p->selected_slot = 0; p->selected_slot = 0;
p->refcount = 1;
p->dl_handle = dlopen(residue, RTLD_NOW); p->dl_handle = dlopen(residue, RTLD_NOW);
if (p->dl_handle == NULL) { if (p->dl_handle == NULL) {
@@ -641,29 +644,27 @@ p11_init(hx509_certs certs, void **data, int flags,
p11_put_session(p, &p->slot); p11_put_session(p, &p->slot);
} }
p->refcount += 1;
*data = p; *data = p;
return 0; return 0;
out: out:
if (p->dl_handle) p11_release_module(p);
dlclose(p->dl_handle);
free(p);
return ret; return ret;
} }
static void static void
p11_release_module(struct p11_module *p) p11_release_module(struct p11_module *p)
{ {
if (p->refcount <= 0) if (p->refcount == 0)
_hx509_abort("pkcs11 refcount to low"); _hx509_abort("pkcs11 refcount to low");
if (--p->refcount > 0) if (--p->refcount > 0)
return; return;
if (p->refcount <= 0)
_hx509_abort("pkcs11 refcount to low");
if (p->dl_handle) if (p->dl_handle)
dlclose(p->dl_handle); dlclose(p->dl_handle);
if (p->slot.name)
free(p->slot.name);
memset(p, 0, sizeof(*P));
free(p); free(p);
} }