com php-src: Fixed bug #70674 (ReflectionFunction::getClosure() leaks memory when used for internal functions): NEWS Zend/zend_clos ures.c
ext/reflection/bug70674.phpt
| From: | Dmitry Stogov | Date: | Thu, 08 Oct 2015 21:45:02 +0000 |
| Subject: | com php-src: Fixed bug #70674 (ReflectionFunction::getClosure() leaks memory when used for internal functions): NEWS Zend/zend_clos ures.c ext/reflection/bug70674.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-89339@lists.php.net to get a copy of this message | ||
Commit: 91fb3a7b2791f97363b085747e98d14efb23a485
Author: Dmitry Stogov <dmitry@zend.com> Fri, 9 Oct 2015 00:45:02 +0300
Parents: fa23bebe94d7149d7bb55d3482f4802546198c24
Branches: PHP-7.0 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=91fb3a7b2791f97363b085747e98d14efb23a485
Log:
Fixed bug #70674 (ReflectionFunction::getClosure() leaks memory when used for internal functions)
Bugs:
https://bugs.php.net/70674
Changed paths:
M NEWS
M Zend/zend_closures.c
A ext/reflection/bug70674.phpt
Diff:
diff --git a/NEWS b/NEWS
index 44adba2..3410a6d 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,8 @@ PHP NEWS
- Reflection:
. Fixed bug #70650 (Wrong docblock assignment). (Marcio)
+ . Fixed bug #70674 (ReflectionFunction::getClosure() leaks memory when used
+ for internal functions). (Dmitry, Bob)
- Standard:
. Fixed bug #70667 (strtr() causes invalid writes and a crashes). (Dmitry)
diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c
index 50aa7d4..9f2292b 100644
--- a/Zend/zend_closures.c
+++ b/Zend/zend_closures.c
@@ -39,6 +39,7 @@ typedef struct _zend_closure {
zend_function func;
zval this_ptr;
zend_class_entry *called_scope;
+ void (*orig_internal_handler)(INTERNAL_FUNCTION_PARAMETERS);
} zend_closure;
/* non-static since it needs to be referenced */
@@ -517,6 +518,15 @@ void zend_register_closure_ce(void) /* {{{ */
}
/* }}} */
+static void zend_closure_internal_handler(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
+{
+ zend_closure *closure = (zend_closure*)EX(func)->common.prototype;
+ closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ OBJ_RELEASE((zend_object*)closure);
+ EX(func)->common.prototype = NULL;
+}
+/* }}} */
+
ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope,
zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
{
zend_closure *closure;
@@ -525,17 +535,16 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func,
zend_class_ent
closure = (zend_closure *)Z_OBJ_P(res);
- memcpy(&closure->func, func, func->type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) :
sizeof(zend_internal_function));
- closure->func.common.prototype = (zend_function*)closure;
- closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
-
if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
/* use dummy scope if we're binding an object without specifying a scope */
/* maybe it would be better to create one for this purpose */
scope = zend_ce_closure;
}
- if (closure->func.type == ZEND_USER_FUNCTION) {
+ if (func->type == ZEND_USER_FUNCTION) {
+ memcpy(&closure->func, func, sizeof(zend_op_array));
+ closure->func.common.prototype = (zend_function*)closure;
+ closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
if (closure->func.op_array.static_variables) {
HashTable *static_variables = closure->func.op_array.static_variables;
@@ -551,6 +560,12 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func,
zend_class_ent
(*closure->func.op_array.refcount)++;
}
} else {
+ memcpy(&closure->func, func, sizeof(zend_internal_function));
+ closure->func.common.prototype = (zend_function*)closure;
+ closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
+ /* wrap internal function handler to avoid memory leak */
+ closure->orig_internal_handler = closure->func.internal_function.handler;
+ closure->func.internal_function.handler = zend_closure_internal_handler;
/* verify that we aren't binding internal function to a wrong scope */
if(func->common.scope != NULL) {
if(scope && !instanceof_function(scope, func->common.scope)) {
diff --git a/ext/reflection/bug70674.phpt b/ext/reflection/bug70674.phpt
new file mode 100644
index 0000000..82cfc53
--- /dev/null
+++ b/ext/reflection/bug70674.phpt
@@ -0,0 +1,8 @@
+--TEST--
+Bug #70674 (ReflectionFunction::getClosure() leaks memory when used for internal functions)
+--FILE--
+<?php
+var_dump(((new ReflectionFunction("strlen"))->getClosure())("hello"));
+?>
+--EXPECT--
+int(5)