Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Signal stack overflow through the return value of php_count_recursive
  • Loading branch information
lazerg committed Aug 11, 2026
commit a11b59fee3faac7b4e9d078c58c8d6f4021c9f70
6 changes: 5 additions & 1 deletion ext/spl/spl_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,11 @@ PHP_METHOD(SplObjectStorage, count)
}

if (mode == PHP_COUNT_RECURSIVE) {
RETURN_LONG(php_count_recursive(&intern->storage));
zend_long count = php_count_recursive(&intern->storage);
if (UNEXPECTED(count < 0)) {
RETURN_THROWS();
}
RETURN_LONG(count);
}

RETURN_LONG(zend_hash_num_elements(&intern->storage));
Expand Down
13 changes: 8 additions & 5 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,14 +614,15 @@ PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */
#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
zend_call_stack_size_error();
return 0;
return -1;
}
#endif

if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(ht)) {
php_error_docref(NULL, E_WARNING, "Recursion detected");
return 0;
/* A user error handler may have thrown. */
return EG(exception) ? -1 : 0;
}
GC_PROTECT_RECURSION(ht);
}
Expand All @@ -630,10 +631,12 @@ PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */
ZEND_HASH_FOREACH_VAL(ht, element) {
ZVAL_DEREF(element);
if (Z_TYPE_P(element) == IS_ARRAY) {
cnt += php_count_recursive(Z_ARRVAL_P(element));
if (UNEXPECTED(EG(exception))) {
zend_long sub_cnt = php_count_recursive(Z_ARRVAL_P(element));
if (UNEXPECTED(sub_cnt < 0)) {
cnt = -1;
break;
}
cnt += sub_cnt;
}
} ZEND_HASH_FOREACH_END();

Expand Down Expand Up @@ -666,7 +669,7 @@ PHP_FUNCTION(count)
cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
} else {
cnt = php_count_recursive(Z_ARRVAL_P(array));
if (UNEXPECTED(EG(exception))) {
if (UNEXPECTED(cnt < 0)) {
RETURN_THROWS();
}
}
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src);
PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src);
PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src);
PHPAPI int php_multisort_compare(const void *a, const void *b);
/* Returns -1 and throws if the array is nested too deeply. */
PHPAPI zend_long php_count_recursive(HashTable *ht);

PHPAPI bool php_array_data_shuffle(php_random_algo_with_state engine, zval *array);
Expand Down
Loading