Commit b34958b877 for openssl.org

commit b34958b877d8fc253c0eb060c45a15527c734c47
Author: Neil Horman <nhorman@openssl.org>
Date:   Thu May 7 17:06:03 2026 -0400

    Don't take reference counts on cache lookups

    Reviewed-by: Bob Beck <beck@openssl.org>
    Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
    MergeDate: Thu Jun 25 21:25:45 2026
    (Merged from https://github.com/openssl/openssl/pull/31143)

diff --git a/crypto/evp/evp_fetch.c b/crypto/evp/evp_fetch.c
index a446f976df..9fa049cdce 100644
--- a/crypto/evp/evp_fetch.c
+++ b/crypto/evp/evp_fetch.c
@@ -351,7 +351,9 @@ inner_evp_generic_fetch(struct evp_method_data_st *methdata,
             if (name_id == 0) {
                 ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED,
                     "Algorithm %s cannot be found", name != NULL ? name : "<null>");
+#ifdef OPENSSL_NO_CACHED_FETCH
                 free_method(method);
+#endif
                 method = NULL;
             } else {
                 meth_id = evp_method_id(name_id, operation_id);
diff --git a/crypto/property/property.c b/crypto/property/property.c
index e807f06995..aa0bb283f1 100644
--- a/crypto/property/property.c
+++ b/crypto/property/property.c
@@ -830,10 +830,18 @@ int ossl_method_store_fetch(OSSL_METHOD_STORE *store,
         }
     }
 fin:
-    if (ret && ossl_method_up_ref(&best_impl->method)) {
+    if (ret) {
         *method = best_impl->method.method;
         if (prov_rw != NULL)
             *prov_rw = best_impl->provider;
+#ifdef OPENSSL_NO_CACHED_FETCH
+        if (!ossl_method_up_ref(&best_impl->method)) {
+            ret = 0;
+            *method = NULL;
+            if (prov_rw != NULL)
+                *prov_rw = NULL;
+        }
+#endif
     } else {
         ret = 0;
     }
@@ -938,9 +946,15 @@ static ossl_inline int ossl_method_store_cache_get_atomic(OSSL_METHOD_STORE *sto

     r = ossl_method_store_atomic_find_in_list(sa, nid, prov, prop_query);

-    if (r != NULL && ossl_method_up_ref(&r->method)) {
+    if (r != NULL) {
         *method = r->method.method;
         res = 1;
+#ifdef OPENSSL_NO_CACHED_FETCH
+        if (!ossl_method_up_ref(&r->method)) {
+            *method = NULL;
+            res = 0;
+        }
+#endif
     }

     return res;