Optimize out unneeded up_ref/free of EVP_CIPHER
authorTomas Mraz <tomas@openssl.org>
Tue, 26 Sep 2023 15:39:50 +0000 (17:39 +0200)
committerTomas Mraz <tomas@openssl.org>
Fri, 29 Sep 2023 13:22:38 +0000 (15:22 +0200)
Fixes #22189

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22198)

crypto/evp/evp_enc.c

index 84ce108b2540ebd52acfb422d457d299f05ed1ec..e1d3eeef641f8f489836670b9f1d58dbcca02f98 100644 (file)
@@ -200,7 +200,12 @@ static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
 #endif
     }
 
-    if (cipher->prov != NULL) {
+    if (!ossl_assert(cipher->prov != NULL)) {
+        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
+        return 0;
+    }
+
+    if (cipher != ctx->fetched_cipher) {
         if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
             return 0;