]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
asn1: update to structure based atomics
authorPauli <pauli@openssl.org>
Wed, 21 Jun 2023 23:30:52 +0000 (09:30 +1000)
committerPauli <pauli@openssl.org>
Sat, 1 Jul 2023 11:18:25 +0000 (21:18 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21260)

crypto/asn1/tasn_utl.c

index be8931cab468867008f5ce187619579e2b753f94..7bd57dc03038d12c4766fe914ac583f13a8f6b73 100644 (file)
@@ -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;
     }