Skip to content

Commit 3a8f260

Browse files
committed
Argument unpacking with Traversables and non-integer keys.
Changed error message, added UPGRADING note and test.
1 parent 6337af0 commit 3a8f260

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

UPGRADING

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ Core:
8080
note that reading and writing a value inside a single expression remains
8181
undefined behavior and may change again in the future.
8282

83+
. Argument unpacking stopped working with Traversables with non-integer keys.
84+
The following code worked in PHP 7.0-7.2 by accident.
85+
86+
function foo(...$args) {
87+
var_dump($args);
88+
}
89+
function gen() {
90+
yield 1.23 => 123;
91+
}
92+
foo(...gen());
93+
94+
Now it generates an exception.
95+
8396
BCMath:
8497
. All warnings thrown by BCMath functions are now using PHP's error handling.
8598
Formerly some warnings have directly been written to stderr.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Argument unpacking does not work with non-integer keys
3+
--FILE--
4+
<?php
5+
function foo(...$args) {
6+
var_dump($args);
7+
}
8+
function gen() {
9+
yield 1.23 => 123;
10+
yield "2.34" => 234;
11+
}
12+
13+
try {
14+
foo(...gen());
15+
} catch (Error $ex) {
16+
echo "Exception: " . $ex->getMessage() . "\n";
17+
}
18+
19+
?>
20+
--EXPECT--
21+
Exception: Cannot unpack Traversable with non-integer keys

Zend/zend_vm_def.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,10 +4522,11 @@ ZEND_VM_C_LABEL(send_again):
45224522
}
45234523

45244524
if (UNEXPECTED(Z_TYPE(key) != IS_LONG)) {
4525-
ZEND_ASSERT(Z_TYPE(key) == IS_STRING);
45264525
zend_throw_error(NULL,
4527-
"Cannot unpack Traversable with string keys");
4528-
zval_ptr_dtor_str(&key);
4526+
(Z_TYPE(key) == IS_STRING) ?
4527+
"Cannot unpack Traversable with string keys" :
4528+
"Cannot unpack Traversable with non-integer keys");
4529+
zval_ptr_dtor(&key);
45294530
break;
45304531
}
45314532
}

Zend/zend_vm_execute.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,10 +1359,11 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_SEND_UNPACK_SPEC_HANDLER(ZEND_
13591359
}
13601360

13611361
if (UNEXPECTED(Z_TYPE(key) != IS_LONG)) {
1362-
ZEND_ASSERT(Z_TYPE(key) == IS_STRING);
13631362
zend_throw_error(NULL,
1364-
"Cannot unpack Traversable with string keys");
1365-
zval_ptr_dtor_str(&key);
1363+
(Z_TYPE(key) == IS_STRING) ?
1364+
"Cannot unpack Traversable with string keys" :
1365+
"Cannot unpack Traversable with non-integer keys");
1366+
zval_ptr_dtor(&key);
13661367
break;
13671368
}
13681369
}

0 commit comments

Comments
 (0)