Skip to content

Commit 4676d9b

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

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.2.8
44

5+
- Standard:
6+
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
57

68
01 Jun 2023, PHP 8.2.7
79

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
@@ -2194,7 +2194,7 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(v
21942194
zend_throw_error(NULL, "Cannot use a scalar value as an array");
21952195
}
21962196

2197-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
2197+
ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
21982198
{
21992199
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
22002200
}

Zend/zend_execute.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ ZEND_API ZEND_COLD void zend_wrong_string_offset_error(void);
7676
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(zend_property_info *info);
7777
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_indirect_modification_error(zend_property_info *info);
7878

79+
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void);
80+
7981
ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg);
8082
ZEND_API ZEND_COLD void zend_verify_arg_error(
8183
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
@@ -3728,7 +3728,12 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
37283728
}
37293729
} else {
37303730
Z_TRY_ADDREF_P(src_zval);
3731-
zend_hash_next_index_insert(Z_ARRVAL_P(dest_zval), src_zval);
3731+
zval *zv = zend_hash_next_index_insert(Z_ARRVAL_P(dest_zval), src_zval);
3732+
if (EXPECTED(!zv)) {
3733+
Z_TRY_DELREF_P(src_zval);
3734+
zend_cannot_add_element();
3735+
return 0;
3736+
}
37323737
}
37333738
zval_ptr_dtor(&tmp);
37343739
} else {
@@ -3737,6 +3742,10 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
37373742
}
37383743
} else {
37393744
zval *zv = zend_hash_next_index_insert(dest, src_entry);
3745+
if (UNEXPECTED(!zv)) {
3746+
zend_cannot_add_element();
3747+
return 0;
3748+
}
37403749
zval_add_ref(zv);
37413750
}
37423751
} ZEND_HASH_FOREACH_END();

0 commit comments

Comments
 (0)