From: Pauli Date: Wed, 21 Jun 2023 23:30:52 +0000 (+1000) Subject: asn1: update to structure based atomics X-Git-Tag: openssl-3.2.0-alpha1~555 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=420ad86a0e35ddbd65dae7e9458e36223af5f140;p=thirdparty%2Fopenssl.git asn1: update to structure based atomics Reviewed-by: Tomas Mraz Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/21260) --- diff --git a/crypto/asn1/tasn_utl.c b/crypto/asn1/tasn_utl.c index be8931cab46..7bd57dc0303 100644 --- a/crypto/asn1/tasn_utl.c +++ b/crypto/asn1/tasn_utl.c @@ -59,7 +59,7 @@ int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value, /* * 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. @@ -68,8 +68,8 @@ int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value, 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) @@ -78,30 +78,34 @@ int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 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; }