diff --git a/mozilla/security/nss/lib/softoken/pkcs11.c b/mozilla/security/nss/lib/softoken/pkcs11.c index 31a5f3aec0d..ba297c368c2 100644 --- a/mozilla/security/nss/lib/softoken/pkcs11.c +++ b/mozilla/security/nss/lib/softoken/pkcs11.c @@ -2455,6 +2455,9 @@ pk11_DBVerify(PK11Slot *slot) return; } +/* forward static declaration. */ +static CK_RV pk11_DestroySlotData(PK11Slot *slot); + /* * initialize one of the slot structures. figure out which by the ID */ @@ -2485,47 +2488,40 @@ PK11_SlotInit(char *configdir,pk11_token_parameters *params, int moduleIndex) #ifdef PKCS11_USE_THREADS slot->slotLock = PZ_NewLock(nssILockSession); - if (slot->slotLock == NULL) { - return CKR_HOST_MEMORY; - } - slot->sessionLock = (PZLock **) - PORT_ZAlloc(slot->numSessionLocks * sizeof(PZLock *)); - if (slot->sessionLock == NULL) { - return CKR_HOST_MEMORY; - } + if (slot->slotLock == NULL) + goto mem_loser; + slot->sessionLock = PORT_ZNewArray(PZLock *, slot->numSessionLocks); + if (slot->sessionLock == NULL) + goto mem_loser; for (i=0; i < slot->numSessionLocks; i++) { slot->sessionLock[i] = PZ_NewLock(nssILockSession); - if (slot->sessionLock[i] == NULL) return CKR_HOST_MEMORY; + if (slot->sessionLock[i] == NULL) + goto mem_loser; } slot->objectLock = PZ_NewLock(nssILockObject); - if (slot->objectLock == NULL) return CKR_HOST_MEMORY; + if (slot->objectLock == NULL) + goto mem_loser; #else slot->slotLock = NULL; - slot->sessionLock = (PZLock **) - PORT_ZAlloc(slot->numSessionLocks * sizeof(PZLock *)); - if (slot->sessionLock == NULL) { - return CKR_HOST_MEMORY; - } + slot->sessionLock = PORT_ZNewArray(PZLock *, slot->numSessionLocks); + if (slot->sessionLock == NULL) + goto mem_loser; for (i=0; i < slot->numSessionLocks; i++) { slot->sessionLock[i] = NULL; } slot->objectLock = NULL; #endif - slot->head = (PK11Session **) - PORT_ZAlloc(slot->sessHashSize*sizeof(PK11Session *)); - if (slot->head == NULL) { - return CKR_HOST_MEMORY; - } - slot->tokObjects = (PK11Object **) - PORT_ZAlloc(slot->tokObjHashSize*sizeof(PK11Object *)); - if (slot->tokObjects == NULL) { - return CKR_HOST_MEMORY; - } + slot->head = PORT_ZNewArray(PK11Session *, slot->sessHashSize); + if (slot->head == NULL) + goto mem_loser; + slot->tokObjects = PORT_ZNewArray(PK11Object *, slot->tokObjHashSize); + if (slot->tokObjects == NULL) + goto mem_loser; slot->tokenHashTable = PL_NewHashTable(64,pk11_HashNumber,PL_CompareValues, SECITEM_HashCompare, NULL, 0); - if (slot->tokenHashTable == NULL) { - return CKR_HOST_MEMORY; - } + if (slot->tokenHashTable == NULL) + goto mem_loser; + slot->password = NULL; slot->hasTokens = PR_FALSE; slot->sessionIDCount = 0; @@ -2555,8 +2551,7 @@ PK11_SlotInit(char *configdir,pk11_token_parameters *params, int moduleIndex) params->noCertDB, params->noKeyDB, params->forceOpen, &slot->certDB, &slot->keyDB); if (crv != CKR_OK) { - /* shoutdown slot? */ - return crv; + goto loser; } if (nsslowcert_needDBVerify(slot->certDB)) { @@ -2576,6 +2571,13 @@ PK11_SlotInit(char *configdir,pk11_token_parameters *params, int moduleIndex) } } return CKR_OK; + +mem_loser: + crv = CKR_HOST_MEMORY; +loser: + /* cleanup partially created SlotData */ + pk11_DestroySlotData(slot); + return crv; } static PRIntn