Skip to content

Zend: Warn when destructuring non-array values #19439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Zend/tests/foreach/foreach_list_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ foreach($array as list(, $a)) {
--EXPECT--
int(1)
int(3)
string(1) "b"
string(1)

Warning: Cannot use string as array in %s on line %d
NULL
NULL
4 changes: 4 additions & 0 deletions Zend/tests/list/bug39304.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ Bug #39304 (Segmentation fault with list unpacking of string offset)
?>
--EXPECTF--
Warning: Uninitialized string offset 0 in %s on line %d

Warning: Cannot use string as array in %s on line %d

Warning: Cannot use string as array in %s on line %d
NULL
NULL
18 changes: 18 additions & 0 deletions Zend/tests/list/destruct_bool.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Destructuring with list() a value of type bool
--FILE--
<?php

$v = true;

list($a, $b) = $v;

var_dump($a, $b);

?>
--EXPECTF--
Warning: Cannot use bool as array in %s on line %d

Warning: Cannot use bool as array in %s on line %d
NULL
NULL
18 changes: 18 additions & 0 deletions Zend/tests/list/destruct_float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Destructuring with list() a value of type float
--FILE--
<?php

$v = 25.64;

list($a, $b) = $v;

var_dump($a, $b);

?>
--EXPECTF--
Warning: Cannot use float as array in %s on line %d

Warning: Cannot use float as array in %s on line %d
NULL
NULL
18 changes: 18 additions & 0 deletions Zend/tests/list/destruct_int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Destructuring with list() a value of type int
--FILE--
<?php

$v = 5;

list($a, $b) = $v;

var_dump($a, $b);

?>
--EXPECTF--
Warning: Cannot use int as array in %s on line %d

Warning: Cannot use int as array in %s on line %d
NULL
NULL
12 changes: 12 additions & 0 deletions Zend/tests/list/destruct_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Destructuring with list() a value of type null
--FILE--
<?php

list($a) = null;

var_dump($a);

?>
--EXPECT--
NULL
17 changes: 17 additions & 0 deletions Zend/tests/list/destruct_object_not_ArrayAccess.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Destructuring with list() a value of type object (that does not implement ArrayAccess)
--FILE--
<?php

$v = new stdClass();

list($a, $b) = $v;

var_dump($a, $b);

?>
--EXPECTF--
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/list/destruct_resource.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Destructuring with list() a value of type resource
--FILE--
<?php

$v = STDERR;

list($a, $b) = $v;

var_dump($a, $b);

?>
--EXPECTF--
Warning: Cannot use resource as array in %s on line %d

Warning: Cannot use resource as array in %s on line %d
NULL
NULL
21 changes: 21 additions & 0 deletions Zend/tests/list/destruct_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Destructuring with list() a value of type string
--FILE--
<?php

$v = 'hi';

list($a, $b, $c) = $v;

var_dump($a, $b, $c);

?>
--EXPECTF--
Warning: Cannot use string as array in %s on line %d

Warning: Cannot use string as array in %s on line %d

Warning: Cannot use string as array in %s on line %d
NULL
NULL
NULL
24 changes: 0 additions & 24 deletions Zend/tests/list/list_003.phpt

This file was deleted.

50 changes: 0 additions & 50 deletions Zend/tests/list/list_005.phpt

This file was deleted.

5 changes: 4 additions & 1 deletion Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -3005,7 +3005,7 @@ static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_UNSET(z
zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_UNSET EXECUTE_DATA_CC);
}

static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type, bool is_list, int slow EXECUTE_DATA_DC)
static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type, bool is_list, bool slow EXECUTE_DATA_DC)
{
zval *retval;

Expand Down Expand Up @@ -3143,6 +3143,9 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
ZVAL_UNDEFINED_OP2();
}
if (is_list && Z_TYPE_P(container) > IS_NULL) {
zend_error(E_WARNING, "Cannot use %s as array", zend_zval_type_name(container));
}
if (!is_list && type != BP_VAR_IS) {
zend_error(E_WARNING, "Trying to access array offset on %s",
zend_zval_value_name(container));
Expand Down
3 changes: 2 additions & 1 deletion ext/opcache/tests/jit/fetch_list_r_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ function test() {
}
test();
?>
--EXPECT--
--EXPECTF--
Warning: Cannot use string as array in %s on line %d
NULL
7 changes: 5 additions & 2 deletions ext/opcache/tests/opt/block_pass_003.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Block Pass 003: Inorrect constant substitution in FETCH_LIST_R
Block Pass 003: Incorrect constant substitution in FETCH_LIST_R
--INI--
opcache.enable=1
opcache.enable_cli=1
Expand All @@ -16,5 +16,8 @@ function test() {
test();
?>
DONE
--EXPECT--
--EXPECTF--
Warning: Cannot use int as array in %s on line %d

Warning: Cannot use int as array in %s on line %d
DONE
4 changes: 4 additions & 0 deletions tests/lang/engine_assignExecutionOrder_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ array(3) {
int(3000)
}
L=100 M=200 N=300

Warning: Cannot use int as array in %s on line %d

Warning: Cannot use int as array in %s on line %d
O= and P=
10 20 40 50 60 70 80

Expand Down
Loading