]> granicus.if.org Git - php/commitdiff
Fix #79145: openssl memory leak
authorChristoph M. Becker <cmbecker69@gmx.de>
Tue, 21 Jan 2020 15:17:25 +0000 (16:17 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Tue, 21 Jan 2020 15:17:25 +0000 (16:17 +0100)
We must increase the refcount of `return_value` only if `cert` is a
resource; this is already done in `php_openssl_evp_from_zval()`,
though.

NEWS
ext/openssl/openssl.c
ext/openssl/tests/bug79145.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 17b73f55b7afbe86bca2800f9e21655db9dd2599..3fd5bb055c1f44bdc5e86ba96f2e438e86f42463 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ PHP                                                                        NEWS
   . Fixed bug #79084 (mysqlnd may fetch wrong column indexes with MYSQLI_BOTH).
     (cmb)
 
+- OpenSSL:
+  . Fixed bug #79145 (openssl memory leak). (cmb, Nikita)
+
 - Reflection:
   . Fixed bug #79115 (ReflectionClass::isCloneable call reflected class
     __destruct). (Nikita)
index 4cc3bd544675aa419faa48473c790b2d3e17549f..7e935ae8ec42d6fe8e7120a80f84ace8db64d086 100644 (file)
@@ -4691,7 +4691,6 @@ PHP_FUNCTION(openssl_pkey_get_public)
                RETURN_FALSE;
        }
        ZVAL_RES(return_value, res);
-       Z_ADDREF_P(return_value);
 }
 /* }}} */
 
@@ -4733,7 +4732,6 @@ PHP_FUNCTION(openssl_pkey_get_private)
                RETURN_FALSE;
        }
        ZVAL_RES(return_value, res);
-       Z_ADDREF_P(return_value);
 }
 
 /* }}} */
diff --git a/ext/openssl/tests/bug79145.phpt b/ext/openssl/tests/bug79145.phpt
new file mode 100644 (file)
index 0000000..3488311
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+Bug #79145 (openssl memory leak)
+--SKIPIF--
+<?php
+if (!extension_loaded('openssl')) die('skip openssl extension not available');
+if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
+?>
+--FILE--
+<?php
+$b = '-----BEGIN PUBLIC KEY-----
+MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDaFhc31WeskqxwI+Si5R/fZrLK
+pJOlABiI3RZfKCHJVrXl3IvcHDFM/BHKUJoSi/ee8GS9iw0G4Z1eCzJdthXxHARh
+j85Q5OliVxOdB1LoTOsOmfFf/fdvpU3DsOWsDKlVrL41MHxXorwrwOiys/r/gv2d
+C9C4JmhTOjBVAK8SewIDAQAC
+-----END PUBLIC KEY-----';
+
+$start = memory_get_usage(true);
+for ($i = 0; $i < 100000; $i++) {
+    $a = openssl_get_publickey($b);
+    openssl_free_key($a);
+}
+$end = memory_get_usage(true);
+var_dump($end <= 1.1 * $start);
+?>
+--EXPECT--
+bool(true)