/*
* Do atomic reference counting. The value 'op' decides what to do.
* If it is +1 then the count is incremented.
- * If |op| is 0, lock is initialised and count is set to 1.
+ * If |op| is 0, count is initialised and set to 1.
* If |op| is -1, count is decremented and the return value is the current
* reference count or 0 if no reference count is active.
* It returns -1 on initialisation error.
int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
{
const ASN1_AUX *aux;
- CRYPTO_REF_COUNT *lck;
CRYPTO_RWLOCK **lock;
+ CRYPTO_REF_COUNT *refcnt;
int ret = -1;
if ((it->itype != ASN1_ITYPE_SEQUENCE)
aux = it->funcs;
if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
return 0;
- lck = offset2ptr(*pval, aux->ref_offset);
lock = offset2ptr(*pval, aux->ref_lock);
+ refcnt = offset2ptr(*pval, aux->ref_offset);
switch (op) {
case 0:
- *lck = ret = 1;
+ if (!CRYPTO_NEW_REF(refcnt, 1))
+ return -1;
*lock = CRYPTO_THREAD_lock_new();
if (*lock == NULL) {
+ CRYPTO_FREE_REF(refcnt);
ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
return -1;
}
+ ret = 1;
break;
case 1:
- if (!CRYPTO_UP_REF(lck, &ret, *lock))
+ if (!CRYPTO_UP_REF(refcnt, &ret))
return -1;
break;
case -1:
- if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
+ if (!CRYPTO_DOWN_REF(refcnt, &ret))
return -1; /* failed */
REF_PRINT_EX(it->sname, ret, (void *)it);
REF_ASSERT_ISNT(ret < 0);
if (ret == 0) {
CRYPTO_THREAD_lock_free(*lock);
*lock = NULL;
+ CRYPTO_FREE_REF(refcnt);
}
break;
}