Repository: dotnet/runtime
File: src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs (C#)

Code before:
            try
            {
                DangerousAddRef(ref addedRef);

                int success = Interop.Crypto.UpRefEvpPkey(this);

                if (success != 1)
                {
                    Debug.Fail("Called UpRefEvpPkey on a key which was already marked for destruction");
                    Exception e = Interop.Crypto.CreateOpenSslCryptographicException();
                    safeHandle.Dispose();
                    throw e;
                }

                // Since we didn't actually create a new handle, copy the handle
                // to the new SafeHandle. DangerousAddRef prevents ReleaseHandle
                // from being called, so handle and ExtraHandle are stable here.
                safeHandle.SetHandle(handle);
                // ExtraHandle is upref'd by UpRefEvpPkey
                safeHandle.ExtraHandle = ExtraHandle;
            }
            catch
            {
                safeHandle.Dispose();
                throw;
            }
            finally
            {
                if (addedRef)
                {
                    DangerousRelease();
                }
            }

            return safeHandle;
        }

        /// <summary>
        ///   Open a named private key using a named OpenSSL <code>ENGINE</code>.
        /// </summary>
        /// <param name="engineName">
        ///   The name of the <code>ENGINE</code> to process the private key open request.
        /// </param>
        /// <param name="keyId">
        ///   The name of the key to open.
        /// </param>
        /// <returns>
        ///   The opened key.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="engineName"/> or <paramref name="keyId"/> is <see langword="null" />.

Review comment (suggestion):
@copilot instead of introducing this `catch` block, move the `safeHandle` declaration after the call to `DangerousAddRef`. We can return it after the extra handle is assigned, we don't need the return to be outside of the try/finally.

Code after:
                // Reliability: Allocate the SafeHandle before calling UpRefEvpPkey so
                // that we don't lose a tracked reference in low-memory situations.
                SafeEvpPKeyHandle safeHandle = new SafeEvpPKeyHandle();

                int success = Interop.Crypto.UpRefEvpPkey(this);

                if (success != 1)
                {
                    Debug.Fail("Called UpRefEvpPkey on a key which was already marked for destruction");
                    Exception e = Interop.Crypto.CreateOpenSslCryptographicException();
                    safeHandle.Dispose();
                    throw e;
                }

                // Since we didn't actually create a new handle, copy the handle
                // to the new SafeHandle. DangerousAddRef prevents ReleaseHandle
                // from being called, so handle and ExtraHandle are stable here.
                safeHandle.SetHandle(handle);
                // ExtraHandle is upref'd by UpRefEvpPkey
                safeHandle.ExtraHandle = ExtraHandle;
                return safeHandle;
            }
            finally
            {
                if (addedRef)
                {
                    DangerousRelease();
                }
            }
        }

        /// <summary>
        ///   Open a named private key using a named OpenSSL <code>ENGINE</code>.
        /// </summary>
        /// <param name="engineName">
        ///   The name of the <code>ENGINE</code> to process the private key open request.
        /// </param>
        /// <param name="keyId">
        ///   The name of the key to open.
        /// </param>
        /// <returns>
        ///   The opened key.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="engineName"/> or <paramref name="keyId"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <paramref name="engineName"/> or <paramref name="keyId"/> is the empty string.
        /// </exception>
        /// <exception cref="CryptographicException">
        ///   the key could not be opened via the specified ENGINE.