Skip to content

Commit 77bb96d

Browse files
committed
Fix bug #71737
Also improve the error message for $this used in parameters.
1 parent ede06b7 commit 77bb96d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
(Nikita Nefedov)
88
. Fixed bug #72038 (Function calls with values to a by-ref parameter don't
99
always throw a notice). (Bob)
10+
. Fixed bug #71737 (Memory leak in closure with parameter named $this).
11+
(Nikita)
1012

1113
- OCI8:
1214
. Fixed bug #71600 (oci_fetch_all segfaults when selecting more than eight

Zend/tests/bug41117_1.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ class foo {
1010
$obj = new foo("Hello world");
1111
?>
1212
--EXPECTF--
13-
Fatal error: Cannot re-assign $this in %sbug41117_1.php on line 3
14-
13+
Fatal error: Cannot use $this as parameter in %s on line %d

Zend/tests/bug71737.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #71737: Memory leak in closure with parameter named $this
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public function method() {
8+
return function($this) {};
9+
}
10+
}
11+
12+
(new Test)->method()(new stdClass);
13+
14+
?>
15+
--EXPECTF--
16+
Fatal error: Cannot use $this as parameter in %s on line %d

Zend/zend_compile.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,8 +4484,9 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
44844484
zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
44854485
ZSTR_VAL(name));
44864486
} else if (zend_string_equals_literal(name, "this")) {
4487-
if (op_array->scope && (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
4488-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
4487+
if ((op_array->scope || (op_array->fn_flags & ZEND_ACC_CLOSURE))
4488+
&& (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
4489+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
44894490
}
44904491
op_array->this_var = var_node.u.op.var;
44914492
}

0 commit comments

Comments
 (0)