Skip to content

Commit ede8adb

Browse files
nielsdosiluuu1994
andauthored
Fix phpGH-11016: Heap buffer overflow in ZEND_ADD_ARRAY_UNPACK_SPEC_HANDLER (php#11021)
Not enough space was reserved for the packed resulting array because of some confusion in the meaning of nr of used slots vs nr of elements. Co-authored-by: Ilija Tovilo <[email protected]>
1 parent 2ef1930 commit ede8adb

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Zend/tests/gh11016.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-11016 (Heap buffer overflow in ZEND_ADD_ARRAY_UNPACK_SPEC_HANDLER)
3+
--FILE--
4+
<?php
5+
function number() {
6+
return 6;
7+
}
8+
9+
// We need to use a function to trick the optimizer *not* to optimize the array to a constant
10+
$x = [number() => 0, ...[1, 1, 1]];
11+
print_r($x);
12+
?>
13+
--EXPECT--
14+
Array
15+
(
16+
[6] => 0
17+
[7] => 1
18+
[8] => 1
19+
[9] => 1
20+
)

Zend/zend_vm_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6128,7 +6128,7 @@ ZEND_VM_C_LABEL(add_unpack_again):
61286128
zval *val;
61296129

61306130
if (HT_IS_PACKED(ht) && (zend_hash_num_elements(result_ht) == 0 || HT_IS_PACKED(result_ht))) {
6131-
zend_hash_extend(result_ht, zend_hash_num_elements(result_ht) + zend_hash_num_elements(ht), 1);
6131+
zend_hash_extend(result_ht, result_ht->nNumUsed + zend_hash_num_elements(ht), 1);
61326132
ZEND_HASH_FILL_PACKED(result_ht) {
61336133
ZEND_HASH_PACKED_FOREACH_VAL(ht, val) {
61346134
if (UNEXPECTED(Z_ISREF_P(val)) &&

Zend/zend_vm_execute.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)