]> granicus.if.org Git - php/commitdiff
Fixed bug #79881
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 22 Jul 2020 08:20:37 +0000 (10:20 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 22 Jul 2020 08:21:24 +0000 (10:21 +0200)
NEWS
ext/openssl/openssl.c
ext/openssl/tests/bug79881.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 1ab7e3d1bc1d749edd4cc887e37134f051ed3291..b28590c2574a5fae24e485a781f7e0aa8692ea6f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,9 @@ PHP                                                                        NEWS
 - Mbstring:
   . Fixed bug #79787 (mb_strimwidth does not trim string). (XXiang)
 
+- OpenSSL:
+  . Fixed bug #79881 (Memory leak in openssl_pkey_get_public()). (Nikita)
+
 - Reflection:
   . Fixed bug #79487 (::getStaticProperties() ignores property modifications).
     (cmb, Nikita)
index 04cb9b0f231301aa40045a49b29185e2e7ef9f45..8a45a6cfa2551614de6a0f6de452f40e05b0df8b 100644 (file)
@@ -3860,10 +3860,6 @@ static EVP_PKEY * php_openssl_evp_from_zval(
                if (!what) {
                        TMP_CLEAN;
                }
-               if (resourceval) {
-                       *resourceval = res;
-                       Z_ADDREF_P(val);
-               }
                if (res->type == le_x509) {
                        /* extract key from cert, depending on public_key param */
                        cert = (X509*)what;
@@ -3887,6 +3883,10 @@ static EVP_PKEY * php_openssl_evp_from_zval(
                                        zval_ptr_dtor_str(&tmp);
                                }
                                /* got the key - return it */
+                               if (resourceval) {
+                                       *resourceval = res;
+                                       Z_ADDREF_P(val);
+                               }
                                return (EVP_PKEY*)what;
                        }
                } else {
diff --git a/ext/openssl/tests/bug79881.phpt b/ext/openssl/tests/bug79881.phpt
new file mode 100644 (file)
index 0000000..915f95b
--- /dev/null
@@ -0,0 +1,45 @@
+--TEST--
+Bug #79881: Memory leak in openssl_pkey_get_public()
+--FILE--
+<?php
+
+$cert = "-----BEGIN CERTIFICATE-----
+MIIE4jC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kZ5/LV3gNn1CAKFDOyjrGNA6r2yyHZ8TqyKguZqpFLVf1Vn7ll8u38hp7mBFO
+/bZGnCdZTtW8Ae8zNQXuFbXoItCk6unGPQsp7/5mHD8nsrqO512zEHuPA+b3v/ZR
+tSwkaJZM3W4LhHWiplEoA0F9CCnBKB1W00LDMZTLa4CRdGJlrjwchG0No+ItGrXZ
+cc+wLMic6UZ1QrXcvo9efj6/bjLHTee39u4SlIcuL20x97993HZyqP5ZSi+QkKfr
+Af441uJNHOFmoClCo6Wb2quNn6F6GvhYxkYcy/CgVP+VOfUBPIF8Ta2KrcI46fAL
+Qq6d/SK0GtjlRZME8h48tocBCGvtcZwmZnOQDLd3M51rmfXKB/6y70PjJqiX9ExR
+CJzpfafv1OaOqtWre6UofQsEx+jP+P7iGqQo+W9XrufnE7IDso+Za98G5cUIj61a
+U6sTKt8w+Ovkxr3UyC/QHdYYJ7nYQfrJ2ml9aAzs1ZlNS8YS0XXpzmpaZhSuhbfM
+F6F0Drg3+os4hVFiimUOjspZ4Su2EpsG86hdqJ/HHxTPPfgVlbBP0mexaRco3KVv
+pgJ4B+Hh5oOOh9TkR+D3ZKzjc6G2+4nhflYjI AD7B080Jshk6TcII1twXD9qBkvm
+9J3nrHWc
+-----END CERTIFICATE-----";
+
+$mem1 = memory_get_usage();
+$read = openssl_x509_read($cert);
+$key = openssl_pkey_get_public($read);
+unset($read, $key);
+$mem2 = memory_get_usage();
+var_dump($mem1 == $mem2);
+
+?>
+--EXPECT--
+bool(true)