Skip to content

Commit

Permalink
Fix bug #71737
Browse files Browse the repository at this point in the history
Also improve the error message for $this used in parameters.
  • Loading branch information
nikic committed Apr 20, 2016
1 parent ede06b7 commit 77bb96d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ PHP NEWS
(Nikita Nefedov)
. Fixed bug #72038 (Function calls with values to a by-ref parameter don't
always throw a notice). (Bob)
. Fixed bug #71737 (Memory leak in closure with parameter named $this).
(Nikita)

- OCI8:
. Fixed bug #71600 (oci_fetch_all segfaults when selecting more than eight
Expand Down
3 changes: 1 addition & 2 deletions Zend/tests/bug41117_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ class foo {
$obj = new foo("Hello world");
?>
--EXPECTF--
Fatal error: Cannot re-assign $this in %sbug41117_1.php on line 3

Fatal error: Cannot use $this as parameter in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/bug71737.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Bug #71737: Memory leak in closure with parameter named $this
--FILE--
<?php

class Test {
public function method() {
return function($this) {};
}
}

(new Test)->method()(new stdClass);

?>
--EXPECTF--
Fatal error: Cannot use $this as parameter in %s on line %d
5 changes: 3 additions & 2 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4484,8 +4484,9 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
ZSTR_VAL(name));
} else if (zend_string_equals_literal(name, "this")) {
if (op_array->scope && (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
if ((op_array->scope || (op_array->fn_flags & ZEND_ACC_CLOSURE))
&& (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
}
op_array->this_var = var_node.u.op.var;
}
Expand Down

0 comments on commit 77bb96d

Please sign in to comment.