com php-src: fix unintentional bc break with compact('this') behavior: ext/standard/array.c ext/standard/tests/array/compact_no_th is.phpt
ext/standard/tests/array/compact_this.phpt
| From: | Nikita Popov | Date: | Thu, 01 Sep 2016 11:01:38 +0000 |
| Subject: | com php-src: fix unintentional bc break with compact('this') behavior: ext/standard/array.c ext/standard/tests/array/compact_no_th is.phpt ext/standard/tests/array/compact_this.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-94227@lists.php.net to get a copy of this message | ||
Commit: 00c2c3a4761e7e4f9d4c4721da684c051588dfc2
Author: Márcio Almada <marcio3w@gmail.com> Thu, 1 Sep 2016 03:50:58 -0400
Committer: Nikita Popov <nikic@php.net> Thu, 1 Sep 2016 13:01:38 +0200
Parents: 2c3408f5cb71c979b15c384459540ac364a2c356
Branches: PHP-7.1 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=00c2c3a4761e7e4f9d4c4721da684c051588dfc2
Log:
fix unintentional bc break with compact('this') behavior
Changed paths:
M ext/standard/array.c
A ext/standard/tests/array/compact_no_this.phpt
A ext/standard/tests/array/compact_this.phpt
Diff:
diff --git a/ext/standard/array.c b/ext/standard/array.c
index f471f2a..0988a86 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -1976,6 +1976,14 @@ static void php_compact_var(HashTable *eg_active_symbol_table, zval
*return_valu
ZVAL_COPY(&data, value_ptr);
zend_hash_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
}
+ if (zend_string_equals_literal(Z_STR_P(entry), "this")) {
+ zend_object *object = zend_get_this_object(EG(current_execute_data));
+ if (object) {
+ GC_REFCOUNT(object)++;
+ ZVAL_OBJ(&data, object);
+ zend_hash_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
+ }
+ }
} else if (Z_TYPE_P(entry) == IS_ARRAY) {
if ((Z_ARRVAL_P(entry)->u.v.nApplyCount > 1)) {
php_error_docref(NULL, E_WARNING, "recursion detected");
diff --git a/ext/standard/tests/array/compact_no_this.phpt
b/ext/standard/tests/array/compact_no_this.phpt
new file mode 100644
index 0000000..df294f0
--- /dev/null
+++ b/ext/standard/tests/array/compact_no_this.phpt
@@ -0,0 +1,25 @@
+--TEST--
+compact() without object context
+--FILE--
+<?php
+
+var_dump(
+ (new class {
+ function test(){
+ return (static function(){ return compact('this'); })();
+ }
+ })->test()
+);
+
+var_dump(compact('this'));
+
+var_dump((function(){ return compact('this'); })());
+
+?>
+--EXPECT--
+array(0) {
+}
+array(0) {
+}
+array(0) {
+}
diff --git a/ext/standard/tests/array/compact_this.phpt b/ext/standard/tests/array/compact_this.phpt
new file mode 100644
index 0000000..f3677e0
--- /dev/null
+++ b/ext/standard/tests/array/compact_this.phpt
@@ -0,0 +1,46 @@
+--TEST--
+compact() with object context
+--FILE--
+<?php
+
+var_dump(
+ (new class {
+ function test(){
+ return compact('this');
+ }
+ })->test()
+);
+
+var_dump(
+ (new class {
+ function test(){
+ return compact([['this']]);
+ }
+ })->test()
+);
+
+var_dump(
+ (new class {
+ function test(){
+ return (function(){ return compact('this'); })();
+ }
+ })->test()
+);
+
+?>
+--EXPECT--
+array(1) {
+ ["this"]=>
+ object(class@anonymous)#1 (0) {
+ }
+}
+array(1) {
+ ["this"]=>
+ object(class@anonymous)#1 (0) {
+ }
+}
+array(1) {
+ ["this"]=>
+ object(class@anonymous)#1 (0) {
+ }
+}