Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.4.25

- Core:
. Fixed bug GH-23088 (Stack overflow when comparing deeply nested arrays).
(Lazizbek Ergashev)

- Date:
. Fixed leak on double DatePeriod::__construct() call. (ilutov)

Expand Down
40 changes: 40 additions & 0 deletions Zend/tests/gh23088.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-23088 (Stack overflow when comparing deeply nested arrays)
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting due to more stack space usage');
}
?>
--INI--
zend.max_allowed_stack_size=256K
--FILE--
<?php

$a = [];
$b = [];

for ($i = 0; $i < 20000; $i++) {
$a = [$a];
$b = [$b];
}

try {
var_dump($a == $b);
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
var_dump($a === $b);
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
Maximum call stack size reached during array comparison
Maximum call stack size reached during array comparison
19 changes: 17 additions & 2 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2417,8 +2417,16 @@ ZEND_API bool ZEND_FASTCALL zend_is_identical(const zval *op1, const zval *op2)
case IS_STRING:
return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
case IS_ARRAY:
return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
if (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2)) {
return 1;
}
#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_throw_error(NULL, "Maximum call stack size reached during array comparison");
return 0;
}
#endif
return zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0;
case IS_OBJECT:
return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
default:
Expand Down Expand Up @@ -3423,6 +3431,13 @@ ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable

ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
{
#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_throw_error(NULL, "Maximum call stack size reached during array comparison");
return ZEND_UNCOMPARABLE;
}
#endif

return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
}
/* }}} */
Expand Down
Loading