Skip to content

Commit a5219c1

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-19303: Unpacking empty packed array into uninitialized array causes assertion failure
2 parents 9999d66 + a08df32 commit a5219c1

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

Zend/tests/array_unpack/gh19303.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-19303 (Unpacking empty packed array into uninitialized array causes assertion failure)
3+
--FILE--
4+
<?php
5+
$a = [0];
6+
unset($a[0]);
7+
var_dump([...$a]);
8+
?>
9+
--EXPECT--
10+
array(0) {
11+
}

Zend/zend_vm_def.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,17 +6325,22 @@ ZEND_VM_C_LABEL(add_unpack_again):
63256325
zval *val;
63266326

63276327
if (HT_IS_PACKED(ht) && (zend_hash_num_elements(result_ht) == 0 || HT_IS_PACKED(result_ht))) {
6328-
zend_hash_extend(result_ht, result_ht->nNumUsed + zend_hash_num_elements(ht), 1);
6329-
ZEND_HASH_FILL_PACKED(result_ht) {
6330-
ZEND_HASH_PACKED_FOREACH_VAL(ht, val) {
6331-
if (UNEXPECTED(Z_ISREF_P(val)) &&
6332-
UNEXPECTED(Z_REFCOUNT_P(val) == 1)) {
6333-
val = Z_REFVAL_P(val);
6334-
}
6335-
Z_TRY_ADDREF_P(val);
6336-
ZEND_HASH_FILL_ADD(val);
6337-
} ZEND_HASH_FOREACH_END();
6338-
} ZEND_HASH_FILL_END();
6328+
/* zend_hash_extend() skips initialization when the number of elements is 0,
6329+
* but the code below expects that result_ht is initialized as packed.
6330+
* We can just skip the work in that case. */
6331+
if (result_ht->nNumUsed + zend_hash_num_elements(ht) > 0) {
6332+
zend_hash_extend(result_ht, result_ht->nNumUsed + zend_hash_num_elements(ht), 1);
6333+
ZEND_HASH_FILL_PACKED(result_ht) {
6334+
ZEND_HASH_PACKED_FOREACH_VAL(ht, val) {
6335+
if (UNEXPECTED(Z_ISREF_P(val)) &&
6336+
UNEXPECTED(Z_REFCOUNT_P(val) == 1)) {
6337+
val = Z_REFVAL_P(val);
6338+
}
6339+
Z_TRY_ADDREF_P(val);
6340+
ZEND_HASH_FILL_ADD(val);
6341+
} ZEND_HASH_FOREACH_END();
6342+
} ZEND_HASH_FILL_END();
6343+
}
63396344
} else {
63406345
zend_string *key;
63416346

Zend/zend_vm_execute.h

Lines changed: 16 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)