Skip to content

Commit 43ffa19

Browse files
nielsdosiluuu1994
andcommitted
Fix GH-11016: Heap buffer overflow in ZEND_ADD_ARRAY_UNPACK_SPEC_HANDLER
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 <ilija.tovilo@me.com>
1 parent c796ce5 commit 43ffa19

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Zend/tests/gh11016.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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(10) {
15+
[6]=>
16+
int(0)
17+
[7]=>
18+
int(1)
19+
[8]=>
20+
int(1)
21+
[9]=>
22+
int(1)
23+
}

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)