diff --git a/Zend/tests/array_unpack/already_occupied.phpt b/Zend/tests/array_unpack/already_occupied.phpt deleted file mode 100644 index 27a18b6ced1af..0000000000000 --- a/Zend/tests/array_unpack/already_occupied.phpt +++ /dev/null @@ -1,41 +0,0 @@ ---TEST-- -Appending to an array via unpack may fail ---SKIPIF-- - ---FILE-- - 0, ...$arr]); - -var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]); - -const ARR = [1, 2, 3]; -const ARR2 = [PHP_INT_MAX-1 => 0, ...ARR]; -var_dump(ARR2); - -?> ---EXPECTF-- -Warning: Cannot add element to the array as the next element is already occupied in %s on line %d -array(2) { - [9223372036854775806]=> - int(0) - [9223372036854775807]=> - int(1) -} - -Warning: Cannot add element to the array as the next element is already occupied in %s on line %d -array(2) { - [9223372036854775806]=> - int(0) - [9223372036854775807]=> - int(1) -} - -Warning: Cannot add element to the array as the next element is already occupied in %s on line %d -array(2) { - [9223372036854775806]=> - int(0) - [9223372036854775807]=> - int(1) -} diff --git a/Zend/tests/array_unpack/with_keys_1.phpt b/Zend/tests/array_unpack/with_keys_1.phpt new file mode 100644 index 0000000000000..4746b7b0fdd2d --- /dev/null +++ b/Zend/tests/array_unpack/with_keys_1.phpt @@ -0,0 +1,11 @@ +--TEST-- +Cannot unpack in array with keys +--FILE-- + 0, ...$ary]; + +?> +--EXPECTF-- +Fatal error: Cannot use unpacking in arrays with explicit keys in %s on line %d diff --git a/Zend/tests/array_unpack/with_keys_2.phpt b/Zend/tests/array_unpack/with_keys_2.phpt new file mode 100644 index 0000000000000..5eaafcf89bfe5 --- /dev/null +++ b/Zend/tests/array_unpack/with_keys_2.phpt @@ -0,0 +1,11 @@ +--TEST-- +Cannot unpack in array with keys (2) +--FILE-- + 4]; + +?> +--EXPECTF-- +Fatal error: Cannot use unpacking in arrays with explicit keys in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 155844e2e082b..10c7cbeb8429d 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6946,6 +6946,8 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ zend_ast *last_elem_ast = NULL; uint32_t i; zend_bool is_constant = 1; + zend_bool has_explicit_keys = 0; + uint32_t unpack_lineno = (uint32_t)-1; if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) { zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression"); @@ -6972,7 +6974,11 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ ) { is_constant = 0; } + if (elem_ast->child[1]) { + has_explicit_keys = 1; + } } else { + unpack_lineno = zend_ast_get_lineno(elem_ast); zend_eval_const_expr(&elem_ast->child[0]); if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) { @@ -6983,6 +6989,11 @@ static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ last_elem_ast = elem_ast; } + if (has_explicit_keys && unpack_lineno != (uint32_t) -1) { + CG(zend_lineno) = unpack_lineno; + zend_error_noreturn(E_COMPILE_ERROR, "Cannot use unpacking in arrays with explicit keys"); + } + if (!is_constant) { return 0; }