com php-src: Fix bug 666222: Zend/tests/closure_049.p hpt Zend/tests/closure_050.phpt Zend/tests/closur e_051.phpt Zend/tests/closure_052.phpt
Zend/tests/closure_053.phpt Zend/tests/closure_ 054.phpt Zend/tests/closure_055.phpt Zend /tests/closure_056.phpt Zend/tests/closure_bug6 6622.phpt
Zend/zend_closures.c Zend/zend_vm _def.h Zend/zend_vm_execute.h
| From: | Stanislav Malyshev | Date: | Mon, 09 Jun 2014 01:59:44 +0000 |
| Subject: | com php-src: Fix bug 666222: Zend/tests/closure_049.p hpt Zend/tests/closure_050.phpt Zend/tests/closur e_051.phpt Zend/tests/closure_052.phpt Zend/tests/closure_053.phpt Zend/tests/closure_ 054.phpt Zend/tests/closure_055.phpt Zend /tests/closure_056.phpt Zend/tests/closure_bug6 6622.phpt Zend/zend_closures.c Zend/zend_vm _def.h Zend/zend_vm_execute.h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-78747@lists.php.net to get a copy of this message | ||
Commit: f47976dd9ba5318eb1e358c816288ecd70975d95
Author: Levi Morrison <levim@php.net> Mon, 12 May 2014 21:53:08 -0600
Committer: Stanislav Malyshev <stas@php.net> Sun, 8 Jun 2014 18:59:44 -0700
Parents: 38343856e19185042c54b2cbcc9185aafc741611
Branches: PHP-5.5 PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=f47976dd9ba5318eb1e358c816288ecd70975d95
Log:
Fix bug 666222
This also adds some smaller, isolated tests related to bug 66622.
Bugs:
https://bugs.php.net/666222
https://bugs.php.net/66622
Changed paths:
A Zend/tests/closure_049.phpt
A Zend/tests/closure_050.phpt
A Zend/tests/closure_051.phpt
A Zend/tests/closure_052.phpt
A Zend/tests/closure_053.phpt
A Zend/tests/closure_054.phpt
A Zend/tests/closure_055.phpt
A Zend/tests/closure_056.phpt
A Zend/tests/closure_bug66622.phpt
M Zend/zend_closures.c
M Zend/zend_vm_def.h
M Zend/zend_vm_execute.h
diff --git a/Zend/tests/closure_049.phpt b/Zend/tests/closure_049.phpt new file mode 100644 index 0000000..684b33d --- /dev/null +++ b/Zend/tests/closure_049.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 049: static::class in static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = static function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; + +var_dump($b->foo()); +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_050.phpt b/Zend/tests/closure_050.phpt new file mode 100644 index 0000000..d43f325 --- /dev/null +++ b/Zend/tests/closure_050.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 050: static::class in non-static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_051.phpt b/Zend/tests/closure_051.phpt new file mode 100644 index 0000000..78b28d7 --- /dev/null +++ b/Zend/tests/closure_051.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 051: static::class in static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = static function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_052.phpt b/Zend/tests/closure_052.phpt new file mode 100644 index 0000000..f878515 --- /dev/null +++ b/Zend/tests/closure_052.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 052: static::class in non-static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_053.phpt b/Zend/tests/closure_053.phpt new file mode 100644 index 0000000..b1d76a2 --- /dev/null +++ b/Zend/tests/closure_053.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 053: self::class in static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = static function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_054.phpt b/Zend/tests/closure_054.phpt new file mode 100644 index 0000000..b2f87d1 --- /dev/null +++ b/Zend/tests/closure_054.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 054: self::class in non-static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_055.phpt b/Zend/tests/closure_055.phpt new file mode 100644 index 0000000..047d72a --- /dev/null +++ b/Zend/tests/closure_055.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 055: self::class in static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = static function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_056.phpt b/Zend/tests/closure_056.phpt new file mode 100644 index 0000000..566de10 --- /dev/null +++ b/Zend/tests/closure_056.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 056: self::class in non-static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_bug66622.phpt b/Zend/tests/closure_bug66622.phpt new file mode 100644 index 0000000..1c9577d --- /dev/null +++ b/Zend/tests/closure_bug66622.phpt @@ -0,0 +1,37 @@ +--TEST-- +Bug 66622: Closures do not correctly capture the late bound class (static::) in some cases + +--FILE-- +<?php +class A { + static function name() { return 'A'; } + function foo() { + $fn = function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } + function bar() { + $fn = static function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } + static function baz() { + $fn = function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } +} +class B extends A { + static function name() { return 'B'; } +} + +function test() { + (new B)->foo(); + (new B)->bar(); + (new B)->baz(); + B::baz(); +} +test(); + +--EXPECT-- +B vs B +B vs B +B vs B +B vs B diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c index fcea56d..0812724 100644 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@ -486,6 +486,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent } } + closure->this_ptr = NULL; /* Invariants: * If the closure is unscoped, it has no bound object. * The the closure is scoped, it's either static or it's bound */ @@ -497,10 +498,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent Z_ADDREF_P(this_ptr); } else { closure->func.common.fn_flags |= ZEND_ACC_STATIC; - closure->this_ptr = NULL; } - } else { - closure->this_ptr = NULL; } } /* }}} */ diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 03cc9c4..f76e529 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -5179,6 +5179,7 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, UNUSED) { USE_OPLINE zend_function *op_array; + int closure_is_static, closure_is_being_defined_inside_static_context; SAVE_OPLINE(); @@ -5187,7 +5188,13 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, UNUSED) zend_error_noreturn(E_ERROR, "Base lambda function for closure not found"); } - zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + closure_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC; + closure_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC; + if (closure_is_static || closure_is_being_defined_inside_static_context) { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC); + } else { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + } CHECK_EXCEPTION(); ZEND_VM_NEXT_OPCODE(); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 511a40b..94026a0 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -6487,6 +6487,7 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_UNUSED_HANDLER { USE_OPLINE zend_function *op_array; + int closure_is_static, closure_is_being_defined_inside_static_context; SAVE_OPLINE(); @@ -6495,7 +6496,13 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_UNUSED_HANDLER zend_error_noreturn(E_ERROR, "Base lambda function for closure not found"); } - zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + closure_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC; + closure_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC; + if (closure_is_static || closure_is_being_defined_inside_static_context) { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC); + } else { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + } CHECK_EXCEPTION(); ZEND_VM_NEXT_OPCODE();
diff --git a/Zend/tests/closure_049.phpt b/Zend/tests/closure_049.phpt new file mode 100644 index 0000000..684b33d --- /dev/null +++ b/Zend/tests/closure_049.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 049: static::class in static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = static function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; + +var_dump($b->foo()); +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_050.phpt b/Zend/tests/closure_050.phpt new file mode 100644 index 0000000..d43f325 --- /dev/null +++ b/Zend/tests/closure_050.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 050: static::class in non-static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_051.phpt b/Zend/tests/closure_051.phpt new file mode 100644 index 0000000..78b28d7 --- /dev/null +++ b/Zend/tests/closure_051.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 051: static::class in static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = static function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_052.phpt b/Zend/tests/closure_052.phpt new file mode 100644 index 0000000..f878515 --- /dev/null +++ b/Zend/tests/closure_052.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 052: static::class in non-static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = function() { + return static::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "B" diff --git a/Zend/tests/closure_053.phpt b/Zend/tests/closure_053.phpt new file mode 100644 index 0000000..b1d76a2 --- /dev/null +++ b/Zend/tests/closure_053.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 053: self::class in static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = static function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_054.phpt b/Zend/tests/closure_054.phpt new file mode 100644 index 0000000..b2f87d1 --- /dev/null +++ b/Zend/tests/closure_054.phpt @@ -0,0 +1,22 @@ +--TEST-- +Closure 054: self::class in non-static closure in non-static method. + +--FILE-- +<?php + +class A { + function foo() { + $f = function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +$b = new B; +var_dump($b->foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_055.phpt b/Zend/tests/closure_055.phpt new file mode 100644 index 0000000..047d72a --- /dev/null +++ b/Zend/tests/closure_055.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 055: self::class in static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = static function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_056.phpt b/Zend/tests/closure_056.phpt new file mode 100644 index 0000000..566de10 --- /dev/null +++ b/Zend/tests/closure_056.phpt @@ -0,0 +1,21 @@ +--TEST-- +Closure 056: self::class in non-static closure in static method. + +--FILE-- +<?php + +class A { + static function foo() { + $f = function() { + return self::class; + }; + return $f(); + } +} + +class B extends A {} + +var_dump(B::foo()); + +--EXPECT-- +string(1) "A" diff --git a/Zend/tests/closure_bug66622.phpt b/Zend/tests/closure_bug66622.phpt new file mode 100644 index 0000000..1c9577d --- /dev/null +++ b/Zend/tests/closure_bug66622.phpt @@ -0,0 +1,37 @@ +--TEST-- +Bug 66622: Closures do not correctly capture the late bound class (static::) in some cases + +--FILE-- +<?php +class A { + static function name() { return 'A'; } + function foo() { + $fn = function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } + function bar() { + $fn = static function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } + static function baz() { + $fn = function() { return static::name(); }; + echo static::name() . ' vs ' . $fn() . "\n"; + } +} +class B extends A { + static function name() { return 'B'; } +} + +function test() { + (new B)->foo(); + (new B)->bar(); + (new B)->baz(); + B::baz(); +} +test(); + +--EXPECT-- +B vs B +B vs B +B vs B +B vs B diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c index fcea56d..0812724 100644 --- a/Zend/zend_closures.c +++ b/Zend/zend_closures.c @@ -486,6 +486,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent } } + closure->this_ptr = NULL; /* Invariants: * If the closure is unscoped, it has no bound object. * The the closure is scoped, it's either static or it's bound */ @@ -497,10 +498,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent Z_ADDREF_P(this_ptr); } else { closure->func.common.fn_flags |= ZEND_ACC_STATIC; - closure->this_ptr = NULL; } - } else { - closure->this_ptr = NULL; } } /* }}} */ diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 03cc9c4..f76e529 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -5179,6 +5179,7 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, UNUSED) { USE_OPLINE zend_function *op_array; + int closure_is_static, closure_is_being_defined_inside_static_context; SAVE_OPLINE(); @@ -5187,7 +5188,13 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, UNUSED) zend_error_noreturn(E_ERROR, "Base lambda function for closure not found"); } - zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + closure_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC; + closure_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC; + if (closure_is_static || closure_is_being_defined_inside_static_context) { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC); + } else { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + } CHECK_EXCEPTION(); ZEND_VM_NEXT_OPCODE(); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 511a40b..94026a0 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -6487,6 +6487,7 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_UNUSED_HANDLER { USE_OPLINE zend_function *op_array; + int closure_is_static, closure_is_being_defined_inside_static_context; SAVE_OPLINE(); @@ -6495,7 +6496,13 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_UNUSED_HANDLER zend_error_noreturn(E_ERROR, "Base lambda function for closure not found"); } - zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + closure_is_static = op_array->common.fn_flags & ZEND_ACC_STATIC; + closure_is_being_defined_inside_static_context = EX(prev_execute_data) && EX(prev_execute_data)->function_state.function->common.fn_flags & ZEND_ACC_STATIC; + if (closure_is_static || closure_is_being_defined_inside_static_context) { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(called_scope), NULL TSRMLS_CC); + } else { + zend_create_closure(&EX_T(opline->result.var).tmp_var, (zend_function *) op_array, EG(scope), EG(This) TSRMLS_CC); + } CHECK_EXCEPTION(); ZEND_VM_NEXT_OPCODE();