From 40b8105cca1fa3cc4b696b059308b6d0f2827fa8 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 28 Sep 2016 23:05:21 +0200 Subject: Fix the constant array case as well --- Zend/tests/array_literal_next_element_error.phpt | 9 +++++++++ Zend/zend_compile.c | 7 +++++-- Zend/zend_compile.h | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Zend/tests/array_literal_next_element_error.phpt b/Zend/tests/array_literal_next_element_error.phpt index 59ffeb02c7..8b4af3cadd 100644 --- a/Zend/tests/array_literal_next_element_error.phpt +++ b/Zend/tests/array_literal_next_element_error.phpt @@ -7,6 +7,9 @@ $i = PHP_INT_MAX; $array = [$i => 42, new stdClass]; var_dump($array); +const FOO = [PHP_INT_MAX => 42, "foo"]; +var_dump(FOO); + ?> --EXPECTF-- Warning: Cannot add element to the array as the next element is already occupied in %s on line %d @@ -14,3 +17,9 @@ array(1) { [%d]=> int(42) } + +Warning: Cannot add element to the array as the next element is already occupied in %s on line %d +array(1) { + [%d]=> + int(42) +} diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 5b67ef8295..d3bec3714e 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5929,7 +5929,7 @@ void zend_do_add_array_element(znode *result, const znode *expr, const znode *of } /* }}} */ -void zend_do_add_static_array_element(zval *result, zval *offset, const zval *expr) /* {{{ */ +void zend_do_add_static_array_element(zval *result, zval *offset, zval *expr) /* {{{ */ { if (offset) { switch (Z_TYPE_P(offset)) { @@ -5952,7 +5952,10 @@ void zend_do_add_static_array_element(zval *result, zval *offset, const zval *ex break; } } else { - zend_hash_next_index_insert(Z_ARRVAL_P(result), &expr, sizeof(zval *), NULL); + if (zend_hash_next_index_insert(Z_ARRVAL_P(result), &expr, sizeof(zval *), NULL) == FAILURE) { + zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied"); + zval_ptr_dtor(&expr); + } } } /* }}} */ diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index e7a73252ee..a0955e34fe 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -598,7 +598,7 @@ void zend_do_shell_exec(znode *result, const znode *cmd TSRMLS_DC); void zend_do_init_array(znode *result, const znode *expr, const znode *offset, zend_bool is_ref TSRMLS_DC); void zend_do_add_array_element(znode *result, const znode *expr, const znode *offset, zend_bool is_ref TSRMLS_DC); -void zend_do_add_static_array_element(zval *result, zval *offset, const zval *expr); +void zend_do_add_static_array_element(zval *result, zval *offset, zval *expr); void zend_do_list_init(TSRMLS_D); void zend_do_list_end(znode *result, znode *expr TSRMLS_DC); void zend_do_add_list_element(const znode *element TSRMLS_DC); -- cgit v1.2.1 From 1f5412982cd65800b061cef4877d0fd87662568b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 28 Sep 2016 23:11:02 +0200 Subject: Handle resource keys in constexpr arrays --- Zend/tests/const_array_with_resource_key.phpt | 15 +++++++++++++++ Zend/zend_compile.c | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 Zend/tests/const_array_with_resource_key.phpt diff --git a/Zend/tests/const_array_with_resource_key.phpt b/Zend/tests/const_array_with_resource_key.phpt new file mode 100644 index 0000000000..2533b787e6 --- /dev/null +++ b/Zend/tests/const_array_with_resource_key.phpt @@ -0,0 +1,15 @@ +--TEST-- +Constexpr arrays should be able to handle resource keys +--FILE-- + 42]; +var_dump(FOO); + +?> +--EXPECTF-- +Strict Standards: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d +array(1) { + [%d]=> + int(42) +} diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index d3bec3714e..f91f029e48 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5940,6 +5940,9 @@ void zend_do_add_static_array_element(zval *result, zval *offset, zval *expr) /* case IS_NULL: zend_symtable_update(Z_ARRVAL_P(result), "", 1, &expr, sizeof(zval *), NULL); break; + case IS_RESOURCE: + zend_error(E_STRICT, "Resource ID#%ld used as offset, casting to integer (%ld)", Z_LVAL_P(offset), Z_LVAL_P(offset)); + /* break missing intentionally */ case IS_LONG: case IS_BOOL: zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), &expr, sizeof(zval *), NULL); -- cgit v1.2.1