From: Márcio Almada Date: Thu, 1 Sep 2016 07:50:58 +0000 (-0400) Subject: fix unintentional bc break with compact('this') behavior X-Git-Tag: php-7.2.0alpha1~1383^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00c2c3a4761e7e4f9d4c4721da684c051588dfc2;p=php fix unintentional bc break with compact('this') behavior --- diff --git a/ext/standard/array.c b/ext/standard/array.c index f471f2a7a4..0988a86f28 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 0000000000..df294f0525 --- /dev/null +++ b/ext/standard/tests/array/compact_no_this.phpt @@ -0,0 +1,25 @@ +--TEST-- +compact() without object context +--FILE-- +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 0000000000..f3677e03e2 --- /dev/null +++ b/ext/standard/tests/array/compact_this.phpt @@ -0,0 +1,46 @@ +--TEST-- +compact() with object context +--FILE-- +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) { + } +}