Skip to content

Commit ded8b7f

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Fix access on NULL pointer in array_merge_recursive()
2 parents 2eee46e + 4676d9b commit ded8b7f

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Access on NULL pointer in array_merge_recursive()
3+
--FILE--
4+
<?php
5+
try {
6+
array_merge_recursive(
7+
['' => [PHP_INT_MAX => null]],
8+
['' => [null]],
9+
);
10+
} catch (Throwable $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
try {
15+
array_merge_recursive(
16+
['foo' => [PHP_INT_MAX => null]],
17+
['foo' => str_repeat('a', 2)],
18+
);
19+
} catch (Throwable $e) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
?>
23+
--EXPECT--
24+
Cannot add element to the array as the next element is already occupied
25+
Cannot add element to the array as the next element is already occupied

Zend/zend_execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(v
22882288
zend_throw_error(NULL, "Cannot use a scalar value as an array");
22892289
}
22902290

2291-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
2291+
ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
22922292
{
22932293
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
22942294
}

Zend/zend_execute.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_invalid_class_constant_type_error(uin
8888

8989
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_object_released_while_assigning_to_property_error(const zend_property_info *info);
9090

91+
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void);
92+
9193
ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg);
9294
ZEND_API ZEND_COLD void zend_verify_arg_error(
9395
const zend_function *zf, const zend_arg_info *arg_info, uint32_t arg_num, zval *value);

ext/standard/array.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3730,7 +3730,12 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
37303730
}
37313731
} else {
37323732
Z_TRY_ADDREF_P(src_zval);
3733-
zend_hash_next_index_insert(Z_ARRVAL_P(dest_zval), src_zval);
3733+
zval *zv = zend_hash_next_index_insert(Z_ARRVAL_P(dest_zval), src_zval);
3734+
if (EXPECTED(!zv)) {
3735+
Z_TRY_DELREF_P(src_zval);
3736+
zend_cannot_add_element();
3737+
return 0;
3738+
}
37343739
}
37353740
zval_ptr_dtor(&tmp);
37363741
} else {
@@ -3739,6 +3744,10 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
37393744
}
37403745
} else {
37413746
zval *zv = zend_hash_next_index_insert(dest, src_entry);
3747+
if (UNEXPECTED(!zv)) {
3748+
zend_cannot_add_element();
3749+
return 0;
3750+
}
37423751
zval_add_ref(zv);
37433752
}
37443753
} ZEND_HASH_FOREACH_END();

0 commit comments

Comments
 (0)